Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40341 Accepted Submission(s): 10922
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 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.
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
NO YES
DFS +奇偶剪枝
#include<stdio.h> int N,M,T,ok,la_x,la_y; char c[8][8]; int hash[8][8]; int num[4][2]={ 0, 1, 1, 0, 0,-1, -1, 0}; int Abs(int x) { if(x>0) return x; else return -x; } void Dfs(int x,int y,int t) { int i,j; if(ok) return ; if(t==T) { if(x==la_x && y==la_y) ok=1; return ; } if(Abs(x-la_x)+Abs(y-la_y)+t>T) return ; for(i=0;i<4;i++) { if( !hash[x+num[i][0]][y+num[i][1]] && (x+num[i][0])>=0 && (x+num[i][0]) <N && (y+num[i][1] >=0) && (y+num[i][1]<M) && (c[x+num[i][0]][y+num[i][1]]=='.'|| c[x+num[i][0]][y+num[i][1]]=='D')) { hash[x+num[i][0]][y+num[i][1]]=1; Dfs(x+num[i][0],y+num[i][1],t+1); hash[x+num[i][0]][y+num[i][1]]=0; } } } int main(void) { // freopen("d:\\in.txt","r",stdin); while(scanf("%d%d%d",&N,&M,&T),(N!=0 && M!=0 && T!=0)) { int i,j,st_x,st_y; ok=1; for(i=0;i<N;i++) scanf("%s",c[i]); for(i=0;i<N;i++) for(j=0;j<M;j++) { hash[i][j]=0; if(c[i][j]=='S') { st_x=i; st_y=j; } if(c[i][j]=='D') { la_x=i; la_y=j; } } if(Abs(st_x-la_x)+Abs(st_y-la_y)>T) ok=0; if((st_x+st_y)%2==(la_x+la_y)%2 && T%2) //奇偶剪枝 ok=0; if((st_x+st_y)%2!=(la_x+la_y)%2 && !T%2) //奇偶剪枝 ok=0; if(ok) { ok=0; hash[st_x][st_y]=1; Dfs(st_x,st_y,0); } if(ok) printf("YES\n"); else printf("NO\n"); } return 0; }