# Include <stdio. h> # Include <stdlib. h> Int n, m, ex, ey, T; Int success; Char maze [10] [10]; Void DFS (int stx, int sty, int DT) { If (Stx <= 0 | STX> N | sty <= 0 | sty> m) Return; If (Stx = ex & sty = ey & dt = T) { Success = 1; Return; } Int temp = (t-Dt)-ABS (ex-STX)-ABS (ey-STY ); If (temp <0 | temp & 1) Return; If (maze [STX] [sty + 1]! = 'X ') { Maze [STX] [sty + 1] = 'X '; DFS (Stx, sty + 1, DT + 1 ); Maze [STX] [sty + 1] = '.'; } If (maze [STX] [sty-1]! = 'X ') { Maze [STX] [sty-1] = 'X '; DFS (Stx, Sty-1, DT + 1 ); Maze [STX] [sty-1] = '.'; } If (maze [STX + 1] [sty]! = 'X ') { Maze [STX + 1] [sty] = 'X '; DFS (Stx + 1, sty, DT + 1 ); Maze [STX + 1] [sty] = '.'; } If (maze [stx-1] [sty]! = 'X ') { Maze [stx-1] [sty] = 'X '; DFS (stx-1, sty, DT + 1 ); Maze [stx-1] [sty] = '.'; } } Int main (void) { Int STX, sty, wall, I, J; While (scanf ("% d", & N, & M, & T) = 3 & (n + M + T )) { Getchar (); Wall = 0; For (I = 1; I <= N; I ++) { For (j = 1; j <= m; j ++) { Scanf ("% C", & maze [I] [J]); If (maze [I] [J] ='s ') { STX = I; Sty = J; } Else if (maze [I] [J] = 'D ') { Ex = I; Ey = J; } Else if (maze [I] [J] = 'X ') Wall ++; } Getchar (); } Success = 0; Maze [STX] [sty] = 'X '; If (N * m-wall <= T) Printf ("NO \ n "); Else { DFS (Stx, sty, 0 ); If (SUCCESS) Printf ("Yes \ n "); Else Printf ("NO \ n "); } } Return 0; }
What is parity pruning?Take the matrix as follows: 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 Take a step from a grid with 0 to a grid with 1. A step from a grid with 1 is bound to a grid with 0. That is: Moving from 0 to 1 must be an odd step, and moving from 0 to 0 must be an even step. Therefore, when the request is from 0 to 0 but requires an odd number of times or from 1 to 0 but requires an even number of times, you can directly determine that the request is not reachable! For example, there is a map: [C-sharp]View plaincopy
- S...
- ....
- ....
- ....
- ... D
The shortest distance from S to D is S = ABS (DX-SX) + ABS (dy-sy ). If a map contains obstacles that cannot pass through: [C-sharp]View plaincopy
- S.. x
- XX. x
- ... X
- . Xxx
- ... D
At this time, the shortest distance s '= S + 4. to bypass the obstacle, no matter the offset points, the offset distance is the shortest distance s plus an even distance. Just like the matrix mentioned above, it requires you to go from 0 to 0. No matter how you go around, it will always be the shortest distance (even step) plus an even step. It requires you to go from 1 to 0, you can only add an even number to the shortest distance (odd step.
You can also search for parity pruning. |