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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
char map[10][10];
int visit[10][10];
int n,m,t,ex,ey,flag;
void dfs(int sx,int sy,int step)
{
int i,dx,dy;
int next[4][2]={{-1,0},{1,0},{0,1},{0,-1}};//方向数组
if(flag)//很重要,可以减少步骤,也是如何使一旦找到需要的路线就可以连续return出
return;
if(sx==ex&&sy==ey&&step==t)
{
flag=1;
return;
}
int tem=t-step-abs(ex-sx)-abs(ey-sy);//剪枝的核心代码
if(tem<0||tem&1)//剪枝:如果剩余的步数已经不足以走到出口,且必须是偶数,偶数-偶数=偶数,奇数-奇数=偶数,
return;
for(i=0;i<4;i++)
{
dx=sx+next[i][0];
dy=sy+next[i][1];
if(dx<0||dy>=m||dy<0||dx>=n)
continue;
if(map[dx][dy]!='X'&&visit[dx][dy]==0)
{
visit[dx][dy]=1;//及时标记,先判断后标记,先确定能不能走 ,在考虑走完标记的问题
dfs(dx,dy,step+1);
if(flag)//若找到结果,及时return
return;
visit[dx][dy]=0;//注意返回标记 以便下次搜索
}
}
return ;
}
int main()
{
int i,j,wall;
int sx,sy;
while(~scanf("%d %d %d",&n,&m,&t))
{
if(n==0&&m==0&&t==0)
break;
flag=0;wall=0;
memset(visit,0,sizeof(visit));
for(i=0;i<n;i++)
{
scanf("%s",map[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(map[i][j]=='S')
{
sx=i;sy=j;
}
if(map[i][j]=='D')
{
ex=i;ey=j;
}
if(map[i][j]=='X')
wall++;
}
}
if(t>n*m-wall-1)//注意时间与墙数和地图的数量关系会减少很多步骤哦
{
printf("NO\n");
continue;
}
visit[sx][sy]=1;//务必记得标记走过的路径
dfs(sx,sy,0);
if(flag==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
1.关于奇偶剪枝:
不利用奇偶剪枝的话一直TLE,现在对于奇偶剪枝不理解,暂时记住公式,写一点自己奇偶剪枝的小理解,t-cnt为目前剩余的时间,abs(x-dx)+abs(y-dy)为从当前改点到终点(门)所需要的最短步数,剩余时间为偶数(奇数),就需要走偶数步(奇数步),
奇数-偶数 = 奇数
而奇数-奇数= 偶数,偶数-偶数=偶数
所以只有当剩余的时间与目前位置到终点的最短步数奇偶性相同时,才有可能恰好在t时刻到大门的地方(因为中间会有墙,需根据题目条件继续判定,奇偶剪枝只是把范围缩小了)
还有就是开头时间步数可移动位置的数量关系,也属于剪枝 勿忘
abs(x-dx)+abs(y-dy)为从当前改点到终点(门)所需要的最短步数,但他会根据总时间的需要进行绕行,奇数步绕行后也是奇数步,偶数步绕行后也是偶数步,故他们之间奇偶性是相同的
2.
此题一开始还有个纠结的问题,就是深搜如何搜到符合条件的路径后就直接退出,一开始不知道怎么解决,因为觉得深搜利用了递归,无法return一下,就直接结束,此时用一个ans来解决就OK了,当搜索多条路径时,记得要还原
需要在可能有变化找到结果标志flag及时return,
3.对于下一步要走的路径(1)先确定下一步要走哪个格 用for函数和方向数组(2)通过book标记数组和map地图数组判断这个位置
是否可以走(越界·是否是墙,是否已经测试或者走过)(3)标记这个位置已经走过(4)进入下一位置,传入这一数组(4)将以标记的数组撤回,以便下次再次搜索(6)将
改变的值再改回来
4.注意数组变量是否混合,每一个数组或者变量名是否改变
5,记得初始化,尤其是while函数