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.
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.
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
PS:之前没有了解过奇偶剪枝,一直超时,后来看到大佬的博客上提到了这个,就去了解了一下。我们设当前点x,y,终点,ex,ey。总时间t,当前走了step步,就还剩下t-step可以走。因为两点之间,直线最短。所以当前点到终点的最小步数sum为sum=abs(ex-x)+abs(ey-y),图中出现了不可路过的障碍物,为了绕开障碍,不管偏移几个点,偏移的距离都是最短距离sum加上一个偶数距离。想要走到终点必须剩下的步数与sum的奇偶性必须相同(这个可以自己想下)。我们令temp=t-step-sum。因为两个奇偶性必须一样,所以temp一定为偶数且temp大于等于0,小于零说明剩下的步骤怎样都走不到终点,直接返回。
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=5e5+10;
const int mod=1e9+7;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
using namespace std;
int n,m,T,flag,ex,ey;
int fx[4][2]={0,1,0,-1,-1,0,1,0};
int vis[10][51];
char maps[10][51];
int check(int x,int y)
{
if(x<0||x>=n||y<0||y>=m||vis[x][y]||maps[x][y]=='X')
return 0;
return 1;
}
void dfs(int x,int y,int t)
{
if(flag)
return ;
if(maps[x][y]=='D'&&t==T)
{
flag=1;
return ;
}
int temp=T-t-abs(ex-x)-abs(ey-y);///奇偶剪枝
if(temp<0||temp&1)
return ;
for(int i=0;i<4;i++)
{
int x1=x+fx[i][0];
int y1=y+fx[i][1];
if(check(x1,y1))
{
vis[x1][y1]=1;
dfs(x1,y1,t+1);
vis[x1][y1]=0;
}
}
}
int main()
{
int x2,y2;
while(~scanf("%d%d%d",&n,&m,&T)&&n+m+T)
{
flag=0;
int wall=0;
for(int i=0;i<n;i++)
{
scanf("%s",maps[i]);
for(int j=0;j<m;j++)
{
if(maps[i][j]=='S')
x2=i,y2=j;
else if(maps[i][j]=='X')
wall++;
else if(maps[i][j]=='D')
ex=i,ey=j;
}
}
if(T>n*m-wall-1)///走完所有空地门还没开,直接跑不出去
{
puts("NO");
continue ;
}
memset(vis,0,sizeof(vis));
vis[x2][y2]=1;
dfs(x2,y2,0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}