Tempter of the Bone
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 87199 Accepted Submission (s): 23744
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 is a trap, and he tried desperately to get out of this maze.
The maze is a rectangle with sizes N by M. There is a door in the maze. At the beginning, the door is 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 in 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 on 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 all test case contains three integers n, m, and T (1 < N, m < 7; 0 < T <), which denote The sizes of the maze and the time at which the door would open, respectively. The next N lines give the maze layout, with all 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 is a not-to-be processed.
Outputfor each test case, print on 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 0
Sample Outputnoyes/*0 1 0 1 0 11 0 1 0 1 00 1 0 1 0 11 0 1 0 1 00 1 0 1 0 1*/
#include <iostream>#include<cmath>#include<cstring>using namespacestd;#defineN 15CharMaps[n][n];intN, M, T, flag, DX, DY, vis[n][n];intdir[4][2] = { {0,1}, {0, -1}, {1,0}, {-1,0}};voidDfsintXintYintk) { if(flag)return ; if(x = = dx && y = = dy && k = =0) flag =1; If it happens, flag = 1;intTMP = ABS (X-DX) + ABS (ydy); if(K < TMP)return ; If the number of steps left is less than the required number of steps TMP, return for(inti =0; I <4; i++) { intNX, NY; NX= x + dir[i][0]; NY= y + dir[i][1]; if(NX >=0&& NX < n && NY >=0&& NY < M && Maps[nx][ny]! ='X'&&!Vis[nx][ny]) {Vis[nx][ny]=1; DFS (NX, NY, K-1); Vis[nx][ny]=0; } }}intMain () {intSX, SY; while(Cin >> n >> m >> T, n+m+t) {flag=0; memset (Vis,0,sizeof(VIS)); Every time to clear 0; for(inti =0; I < n; i++) for(intj =0; J < M; J + +) {cin>>Maps[i][j]; if(Maps[i][j] = ='S') SX= i, sy =J; Else if(Maps[i][j] = ='D') DX= i, dy =J; } intQ = (sx+sy)%2, W = (dx+dy)%2; Vis[sx][sy]=1; if((q+w)%2= = t%2)//pruning, if the required number of steps reached and the given T parity is not the same, do not perform DFS, no DFS (SX, SY, T); if(flag) cout<<"YES"<<Endl; Elsecout <<"NO"<<Endl; } return 0;}
Tempter of the Bone