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
NOYES
搜索 加 奇偶剪枝
如果走偶数步要求的步数是奇数,或者走奇数步要求的步数是偶数,都明显不可行
奇数-偶数 = 奇数,偶数-奇数 = 奇数;
奇数-奇数= 偶数,偶数-偶数=偶数;
因此所剩步数减去最小步数必须是偶数;
此外,最小步数大于所剩步数也是不可以的
#include<stdio.h> #include<string.h> #include<math.h> int n,m,s; int a1,a2,b1,b2; int t[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; char a[10][10]; int v[10][10]; int flag; void dfs(int x,int y,int step) { if((int)fabs(x-b1)+(int)fabs(y-b2)>s-step||(s-step-(int)fabs(x-b1)-(int)fabs(y-b2))%2!=0) //剪枝 { return; } if(flag==1) return ; if(step==s) { if(x==b1&&y==b2) { flag=1; return ; } } for(int i=0; i<4; i++) { int tx=x+t[i][0]; int ty=y+t[i][1]; if(tx>=0&&tx<n&&ty>=0&&ty<m&&a[tx][ty]!='X'&&v[tx][ty]==0) { v[tx][ty]=1; dfs(tx,ty,step+1); v[tx][ty]=0; } } } int main() { while(~scanf("%d%d%d",&n,&m,&s)) { if(n==0&&m==0&&s==0) break; flag=0; int sum=0; memset(a,0,sizeof(a)); memset(v,0,sizeof(v)); for(int i=0; i<n; i++) { scanf("%s",a[i]); for(int j=0; j<m; j++) { if(a[i][j]=='S') { a1=i; a2=j; } if(a[i][j]=='D') { b1=i; b2=j; } if(a[i][j]=='X') sum++; } } if(n*m-sum<=s) //所能走的地方小于要走的步数· { printf("NO\n"); continue; } v[a1][a2]=1; dfs(a1,a2,0); if(flag==1) printf("YES\n"); else printf("NO\n"); } return 0; }
本文介绍了一个迷宫逃生问题,通过深度优先搜索算法结合奇偶剪枝策略帮助一只小狗在限定时间内找到出口。文章详细展示了算法的实现过程,并通过样例输入输出验证了算法的有效性。

被折叠的 条评论
为什么被折叠?



