标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 90751 Accepted Submission(s): 24692
1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 int ac[4][2] = {0, 1, 0, -1, -1, 0, 1, 0}; 7 char map[10][10]; int vis[10][10]; 8 int n, m, fx, fy, t; 9 bool Judge; 10 void Dfs(int x, int y, int k, int t) 11 { 12 int xx, yy; 13 vis[x][y] = 1; 14 int dis = (t-k)-(abs(x-fx)+abs(y-fy)); //奇偶剪枝。 15 if(dis < 0 || dis%2) 16 return; 17 if(k >= t) 18 { 19 if(x == fx && y == fy) 20 Judge = true; 21 } 22 else 23 { 24 for(int i = 0; i < 4 && Judge == false/*!Judge*/; i++) //小剪枝, 有逃生路径了就不再执行 25 { 26 xx = x + ac[i][0]; 27 yy = y + ac[i][1]; 28 if(map[xx][yy] != ‘X‘ && xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy]) 29 { 30 Dfs(xx, yy, k + 1, t); 31 vis[xx][yy] = 0; //回溯; 32 } 33 } 34 } 35 } 36 int main() 37 { 38 while(~scanf("%d %d %d", &n, &m, &t), n+m+t) 39 { 40 Judge = false; 41 // memset(map, 0, sizeof(map)); 42 memset(vis, 0, sizeof(vis)); 43 int x, y; 44 for(int i = 0; i < n; i++) 45 { 46 cin >> map[i]; 47 for(int j = 0; j < m; j++) 48 { 49 if(map[i][j] == ‘S‘) 50 { 51 x = i; 52 y = j; 53 } 54 if(map[i][j] == ‘D‘) 55 { 56 fx = i; 57 fy = j; 58 } 59 } 60 } 61 Dfs(x, y, 0, t); 62 if(Judge) 63 puts("YES"); 64 else 65 puts("NO"); 66 } 67 return 0; 68 }
杭电1010--Tempter of the Bone(Dfs+剪枝)
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4746122.html