Address https://vjudge.net/problem/ZOJ-2110
The initial look that is simple DFS or BFS
Later card for a long time only to find that exactly T seconds to reach .......
Do pruning modify the judgment condition for a successful exit == T and the ac
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <string> 5 #include <memory.h> 6 7 8 using namespace std; 9 10 int n, m, t; 11 12 const int MAX_SIZE = 90; 13 14 char G[MAX_SIZE][MAX_SIZE]; 15 16 int currx, curry; 17 18 int addx[] = { 0,0,1,-1 }; 19 int addy[] = { 1,-1,0,0 }; 20 21 int endX; 22 int endY; 23 24 25 26 int Dfs(int x, int y, int limit) 27 { 28 int count = 0; 29 30 if (x < 0 || x >= n || y < 0 || y >= m) { 31 return 0; 32 } 33 if (G[x][y] == 'X') 34 return 0; 35 if (G[x][y] == 'D' && limit == t) 36 return 1; 37 if (limit > t) 38 return 0; 39 40 int temp = (t - limit) - abs(x - endX) - abs(y - endY); 41 42 if (temp < 0 ) 43 return 0; 44 45 46 for ( int i = 0 ; to < 4 ; to ++ ) { 47 int newx = addx [i] + x; 48 int OF NEW = addy [i] + that; 49 char oldV = G [x] [y]; 50 G [x] [y] = ' X ' ; 51 os (DFS (newx, OF NEW, limit + 1 ) == 1 ) 52 return 1 ; 53 G [x] [y] = oldV; 54 } 55 56 return 0; 57 } 58 59 60 int main() 61 { 62 while (scanf("%d%d%d", &n, &m, &t)) { 63 if (n == m && m== t && t == 0) 64 break; 65 66 memset(G, 0, sizeof G); 67 68 for (int i = 0; i < n; i++) 69 for (int j = 0; j < m; j++) { 70 cin >> G[i][j]; 71 if (G[i][j] == 'S') { 72 currx = i; 73 curry = j; 74 } 75 else if (G[i][j] == 'D') { 76 endX = i; 77 endY = j; 78 } 79 } 80 if (Dfs(currx, curry, 0) != 0) 81 printf("YES\n"); 82 else 83 printf("NO\n"); 84 } 85 86 return 0; 87 }