标签:input sci print esc accept tip test one memory
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 115817 Accepted Submission(s): 31439
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 7 char maze[10][10]; 8 bool visited[10][10]; 9 int N, M, T, sx, sy, ex, ey; 10 bool flag; 11 12 int dir_x[4] = {-1, 0, 1, 0}; 13 int dir_y[4] = {0, 1, 0, -1}; 14 15 void dfs(int i, int j, int cnt){ 16 if(flag == true) 17 return; 18 if((maze[i][j] == ‘D‘) && (cnt == T)){ 19 flag = true; 20 return; 21 } 22 if(cnt > T) 23 return; 24 int temp = T - cnt - abs(i - ex) - abs(j - ey);//奇偶剪枝的关键 25 if((temp < 0) || (temp & 1)) 26 return; 27 for(int k = 0; k < 4; k++){ 28 int dx = i + dir_x[k]; 29 int dy = j + dir_y[k]; 30 if((dx >= 0) && (dx < N) && (dy >= 0) && (dy < M) && (maze[dx][dy] != ‘X‘) && (!visited[dx][dy])) { 31 visited[dx][dy] = true; 32 dfs(dx, dy, cnt+1); 33 visited[dx][dy] = false; 34 } 35 } 36 return; 37 } 38 39 int main(){ 40 while(cin >> N >> M >> T){ 41 if(N == 0 && M == 0 && T == 0) 42 break; 43 int xnum = 0;//X即墙的数目 44 memset(visited, false, sizeof(visited)); 45 for(int i = 0; i < N; i++){ 46 for(int j = 0; j < M; j++){ 47 cin >> maze[i][j]; 48 if(maze[i][j] == ‘S‘){ 49 sx = i; 50 sy = j; 51 } 52 if(maze[i][j] == ‘D‘){ 53 ex = i; 54 ey = j; 55 } 56 if(maze[i][j] == ‘X‘){ 57 xnum++; 58 } 59 } 60 } 61 if((N * M - xnum) <= T){//可通过的格子数小于T,狗肯定没法活 62 cout << "NO" << endl; 63 continue; 64 } 65 flag = false; 66 visited[sx][sy] = true;//起点视为以访问过 67 dfs(sx, sy, 0); 68 if(flag == true) 69 cout << "YES" << endl; 70 else 71 cout << "NO" << endl; 72 } 73 return 0; 74 }
标签:input sci print esc accept tip test one memory
原文地址:http://www.cnblogs.com/qinduanyinghua/p/6409235.html