Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 71376 Accepted Submission(s): 19665
Problem Description
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/* 2014-11-27 16:26 更新 尼玛,好坑爹,,,小细节一直错 */ #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; struct Node{ int x,y; }start,end; char map[102][102]; int move[4][2]={0,1,0,-1,1,0,-1,0}; int n,m,k; bool Judge(Node temp){ if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m&&map[temp.x][temp.y]!='X'){ return true; } return false; } bool DFS(Node cur,int time){ if(cur.x==end.x&&cur.y==end.y&&time==k) return true; if(((abs(end.x-cur.x)+abs(end.y-cur.y))&1) != ((k-time)&1))return false; Node temp; //printf("steps=%d\n",cur.steps); for(int i=0;i<4;i++){ temp.x=cur.x+move[i][0]; temp.y=cur.y+move[i][1]; if(Judge(temp)){ map[temp.x][temp.y]='X'; if(DFS(temp,time+1)) return true; map[temp.x][temp.y]='.'; } } return false; } int main(){ int i,j; while(scanf("%d%d%d",&n,&m,&k)!=EOF){ if(n+m+k==0) break; memset(map,0,sizeof(map)); for(i=0;i<n;i++){ scanf("%s",map[i]); for(j=0;j<m;j++){ if(map[i][j]=='S'){ start.x=i;start.y=j; map[i][j]='.'; } if(map[i][j]=='D'){ end.x=i;end.y=j; map[i][j]='.'; } } } map[start.x][start.y]='X'; //此处不标记会出错 if(DFS(start,0)){ printf("YES\n"); }else{ printf("NO\n"); } } return 0; }
/* 回溯算法搞错了,,一直得不到结果,,,郁闷 关键点:剪枝方法 奇偶剪枝 Time:2014-8-25 18:16 */ #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; struct Node{ int x,y; }Start,End; int N,M; int Limit; bool found; int move[4][2]={0,1,1,0,-1,0,0,-1}; char map[8][8]; bool Judge(Node cur){ if(cur.x>=0&&cur.x<N&&cur.y>=0&&cur.y<M&&map[cur.x][cur.y]!='X') return true; return false; } void DFS(Node cur,int Time){ //printf("Time %d\n",Time); if(found)return; if(cur.x==End.x&&cur.y==End.y&&Time==Limit){ found=true; return ;} if( ((Limit-Time)&1) != ((abs(End.x-cur.x)+abs(End.y-cur.y))&1) ) { //printf(" %d %d %d %d %d %d\n",Limit,Time,cur.x,cur.y,End.x,End.y); return ; } Node Next;//不能直接拿 cur+=move[][],这样会改变 cur的值,没法回溯 for(int i=0;i<4;i++){ Next.x=cur.x+move[i][0]; Next.y=cur.y+move[i][1]; if(Judge(Next)){ //Time++; map[Next.x][Next.y]='X'; //printf("cur.x %d cur.y %d Time %d\n",cur.x,cur.y,Time); DFS(Next,Time+1); //Time--; map[Next.x][Next.y]='.'; } } return; } void solve(){ while(scanf("%d%d%d",&N,&M,&Limit),N+M+Limit){ int sum=0; for(int i=0;i<N;i++){ scanf("%s",&map[i]); for(int j=0;j<M;j++){ if(map[i][j]=='S'){ map[i][j]='.'; Start.x=i; Start.y=j; } if(map[i][j]=='D'){ map[i][j]='.'; End.x=i; End.y=j; } if(map[i][j]=='.') sum++; } } if(sum<Limit){ printf("NO\n"); continue; } //printf("%d %d\n",Start.x,Start.y); found=0; map[Start.x][Start.y]='X'; DFS(Start,0); if(found) printf("YES\n"); else printf("NO\n"); } } int main(){ solve(); }