M - Tempter of the Bone
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES
//走迷宫,第一行是 n ,m (都小于7) 然后是一个时间 t ,然后是地图,S 是起点,D 是终点,走一步一秒,且不能回头,问是否能在 t 时刻恰好到终点
奇偶剪枝,
想一下就知道,怎么走到终点的步数奇偶性都不会改变
abs(s_x-e_x)+abs(s_y-e_y) 这个是起点到终点无视障碍的最短步数,如果奇偶不同,不需要 dfs 。直接 NO。
补充一下,奇-奇=偶 , 偶-偶=偶
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 7 char map[10][10]; 8 bool vis[10][10]; 9 int s_x,s_y,e_x,e_y; 10 int a,b,t,ok; 11 12 bool check(int x,int y) 13 { 14 if (x<=0||x>a||y<=0||y>b) 15 return 0; 16 if (map[x][y]=='X'||vis[x][y]||ok) 17 return 0; 18 return 1; 19 } 20 21 void dfs(int x,int y,int time) 22 { 23 vis[x][y]=1; 24 //printf("(%d,%d) ",x,y); 25 if (x==e_x&&y==e_y&&time==t)//满足条件到终点 26 { 27 ok=1; 28 return; 29 } 30 31 int temp=t-time-(abs(x-e_x)+abs(y-e_y)); 32 33 if (temp<0)//剩余步数小于 0 34 return; 35 36 int n_x,n_y; 37 38 n_x=x,n_y=y+1; 39 if (check(n_x,n_y)) 40 { 41 dfs(n_x,n_y,time+1); 42 vis[n_x][n_y]=0; 43 } 44 45 n_x=x+1,n_y=y; 46 if (check(n_x,n_y)) 47 { 48 dfs(n_x,n_y,time+1); 49 vis[n_x][n_y]=0; 50 } 51 52 n_x=x,n_y=y-1; 53 if (check(n_x,n_y)) 54 { 55 dfs(n_x,n_y,time+1); 56 vis[n_x][n_y]=0; 57 } 58 59 n_x=x-1,n_y=y; 60 if (check(n_x,n_y)) 61 { 62 dfs(n_x,n_y,time+1); 63 vis[n_x][n_y]=0; 64 } 65 } 66 67 int main() 68 { 69 int i,j; 70 while (scanf("%d%d%d",&a,&b,&t)&&a+b+t) 71 { 72 getchar(); 73 int wall=0; 74 for (i=1;i<=a;i++) 75 { 76 for (j=1;j<=b;j++) 77 { 78 scanf("%c",&map[i][j]); 79 if (map[i][j]=='S') 80 { 81 s_x=i; 82 s_y=j; 83 } 84 if (map[i][j]=='D') 85 { 86 e_x=i; 87 e_y=j; 88 } 89 if (map[i][j]=='X') 90 wall++; 91 } 92 getchar(); 93 } 94 95 if (a*b-wall<=t)//所有路都走了还到不了终点,注意是 <=t ,可以少300ms 96 { 97 printf("NO "); 98 continue; 99 } 100 101 ok=0; 102 memset(vis,0,sizeof(vis)); 103 int temp=t-(abs(s_x-e_x)+abs(s_y-e_y)); 104 105 if (temp%2==0)//奇偶性相同才bfs 106 dfs(s_x,s_y,0); 107 108 if (ok) 109 printf("YES "); 110 else 111 printf("NO "); 112 } 113 return 0; 114 }