Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 108022 Accepted Submission(s): 29364
Problem DescriptionThe 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.
InputThe 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.
OutputFor each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input4 4 5S.X...X...XD....3 4 5S.X...X....D0 0 0Sample OutputNO YES
题意:能否恰好的时间从S走到D。
奇偶剪枝:根据题目,doggie必须在第t秒到达门口。也就是需要走t-1步。设doggie开始的位置为(sx,sy),目标位置为(ex,ey).如果abs(ex-x)+abs(ey-y)为偶数,则abs(ex-x)和abs(ey-y)奇偶性相同,所以需要走偶数步;
当abs(ex-x)+abs(ey-y)为奇数时,则abs(ex-x)和abs(ey-y)奇偶性不同,到达目标位置就需要走奇数步。先判断奇偶性再搜索可以节省很多时间,不然的话容易超时。t-sum为到达目标位置还需要多少步。因为题目要求doggie必须在第t秒到达门的位置,所以(t-step)和abs(ex-x)+abs(ey-y)的奇偶性必然相同。因此temp=(t-step)-abs(ex-x)+abs(ey-y)必然为偶数。
附AC代码:
1 #include<iostream> 2 #include<cstring> 3 #include<cmath> 4 using namespace std; 5 6 int flag,sx,sy,ex,ey,num,wall; 7 int n,m,t,vis[10][10]; 8 int d[4][2]={{0,-1},{0,1},{1,0},{-1,0}}; 9 char map[10][10]; 10 11 void DFS(int x,int y,int sum){ 12 int xx,yy; 13 if(flag==1) 14 return ; 15 if(x==ex&&y==ey&&sum==t){ 16 flag=1; 17 return; 18 } 19 int temp=abs(x-ex)+abs(y-ey); 20 if(temp>t-sum||(temp+t-sum)&1) 21 return ; 22 for(int i=0;i<4;i++){ 23 xx=x+d[i][0]; 24 yy=y+d[i][1]; 25 if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&map[xx][yy]!='X'){ 26 vis[xx][yy]=1; 27 DFS(xx,yy,sum+1); 28 vis[xx][yy]=0; 29 } 30 } 31 } 32 33 int main(){ 34 while(cin>>n>>m>>t){ 35 if(n==0&&m==0&&t==0) 36 break; 37 wall=0; 38 for(int i=0;i<n;i++){ 39 cin>>map[i]; 40 for(int j=0;j<m;j++){ 41 if(map[i][j]=='S'){ 42 sx=i; 43 sy=j; 44 } 45 if(map[i][j]=='D'){ 46 ex=i; 47 ey=j; 48 } 49 if(map[i][j]=='X') 50 wall++; 51 } 52 } 53 if(n*m-num<=t){ 54 cout<<"NO"<<endl; 55 continue; 56 } 57 flag=0; 58 memset(vis,0,sizeof(vis)); 59 vis[sx][sy]=1; 60 DFS(sx,sy,0); 61 if(flag) 62 cout<<"YES"<<endl; 63 else 64 cout<<"NO"<<endl; 65 } 66 return 0; 67 }