The question is: Today Xiaoyun and Xiaotang are playing a grid jumping game. Xiaoyun said to Xiaotang:
I will draw some grids on the ground, and the following symbols will appear in the grids.
'S' indicates that you want to start from this grid
'X' indicates that the grid cannot be jumped.
'.' Indicates that this grid can be jumped.
'D' indicates that the entry to this grid is over.
Then I will tell you the number of steps you want to skip. Each hop takes one step. Can you just jump to the end point?
Input
The input contains multiple groups of test data. The first row of each group of test data has three numbers: n, m, S (1 <n, m <7; 0 <S <50 ),
The number of rows, columns, and steps of the grid are displayed. If n, m, and s are both 0, the input ends.
Output
If you can just jump to the end, output yes. If not, output No.
It is worth noting that the parity between the path length from one point in the Matrix to another is certain.
Also, if you want to use DFS, be sure to note that this is a backtracking process and should be marked
BFSCodeAs follows (the Shortest Path determines the relationship between the shortest path and S, including the size and parity)
# Include <cstdio> # include <cstring> # include <queue> using namespace STD; const int n = 10; const int INF = 1000000; int n, m, S; int G [N] [N], d [10] [10]; struct node {int I, J ;}; void BFS (INT S1, int S2) {node no; queue <node> q; No. I = S1, no. j = S2; q. push (NO); bool vis [N] [N]; memset (VIS, 0, sizeof (VIS); For (INT I = 0; I <N; ++ I) for (Int J = 0; j <m; ++ J) d [I] [J] = inf; d [S1] [s2] = 0; vis [s 1] [s2] = true; while (! Q. empty () {node u = Q. front (); q. pop (); int x = u. i, y = u. j; If (G [x] [Y] = 'D') break; If (x> 0 &&! Vis [x-1] [Y] & G [x-1] [Y]! = 'X' & D [x-1] [Y]> d [x] [Y] + 1) {No. I = X-1, no. j = y; q. push (NO); vis [x-1] [Y] = 1; d [x-1] [Y] = d [x] [Y] + 1 ;} if (Y> 0 &&! Vis [x] [Y-1] & G [x] [Y-1]! = 'X' & D [x] [Y-1]> d [x] [Y] + 1) {No. I = X, No. j = Y-1; q. push (NO); vis [x] [Y-1] = 1; d [x] [Y-1] = d [x] [Y] + 1 ;} if (x <n-1 &&! Vis [x + 1] [Y] & G [x + 1] [Y]! = 'X' & D [x + 1] [Y]> d [x] [Y] + 1) {No. I = x + 1, No. j = y; q. push (NO); vis [x + 1] [Y] = 1; d [x + 1] [Y] = d [x] [Y] + 1 ;} if (Y <M-1 &&! Vis [x] [Y + 1] & G [x] [Y + 1]! = 'X' & D [x] [Y + 1]> d [x] [Y] + 1) {No. I = X, No. j = Y + 1; q. push (NO); vis [x] [Y + 1] = 1; d [x] [Y + 1] = d [x] [Y] + 1 ;}}} int main () {While (scanf ("% d", & N, & M, & S )! = EOF & (N | M | S) {getchar (); int S1, S2, E1, E2; For (INT I = 0; I <N; getchar (), ++ I) for (Int J = 0; j <m; ++ J) {scanf ("% C ", & G [I] [J]); If (G [I] [J] = 's') S1 = I, S2 = J; if (G [I] [J] = 'D') e1 = I, E2 = J;} BFS (S1, S2 ); // printf ("% d \ n", d [E1] [E2]); If (d [E1] [E2]> S | (S % 2 )! = (D [E1] [E2] % 2) printf ("NO \ n"); else printf ("Yes \ n ");}}