Tempter of the Bone
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
题意:根据地图,'S'为开始位置,'D'为门的位置,' . '为空地,'X'为墙,不能经过,
问:在指定的时间,是否能到达'门'的位置.
注意:路不可以重复经过,时间也要刚好是 t ,不能少.
思路:还是DFS,不能用BFS,因为BFS求的是最短路径,而此题的路径不一定最短.
剪枝是关键,奇偶剪枝.
奇偶剪枝原理:
要理解奇偶剪枝,先了解一下曼哈顿距离,从一个点到达另外一个点的最短路径长度(时间)可以根据两点坐标求出,
路径长度(非最短)与最短路径的长度同奇偶,它们的差一定是偶数!举个例子,就像两个偶数的差差是偶数,两个个数的差也是偶数.
本题还有一个剪枝:n*m-wall与t的关系,wall为'X'的数量,解释一下,n*m为区域总数,
其中t-cnt味剩余的步数或者说时间,另其为T
s1+s2为剩余步数,另其为S
如果走偶数步要求的时间是奇数,或者走奇数步要求的时间是偶数,都明显不可行
而轻易得出奇数-偶数 = 奇数,反之亦然
而奇数-奇数= 偶数,偶数-偶数=偶数
所以tem必须为偶
一个改了很多遍最后发现是全局变量定义的问题的程序。。。
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
using namespace std;
char c[10][10];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//四个方向
int n,m,t,sx,sy,ex,ey;
bool flag;
void dfs(int x,int y,int cnt)//深搜 cnt时间
{
int i;
if(x>=n||y>=m||x<0||y<0)//出界
return ;
if(cnt==t&&x==ex&&y==ey)
{//到达终点 时间也正好
flag=true;
return;
}
if(flag)
return ;//一旦符合马上结束循环
int s1=x-ex;
int s2=y-ey;//起点与终点坐标距离
if(s1<0)
s1=-s1;
if(s2<0)
s2=-s2;
//s1+s2 t-cnt 奇偶性应该一致
//tem=t-cnt-s1-s2;
int temp=(t-cnt)-(s1+s2);
if(temp<0||temp%2==1)//奇偶性剪枝 剩余时间应该大于0
return;
for(i = 0; i<4; i++)
{
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(c[xx][yy]!='X')
{
c[xx][yy]='X';
dfs(xx,yy,cnt+1);
c[xx][yy]='.';
if(flag)
return ;
}
}
return ;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&t))
{
if(n==0&&m==0&&t==0)
break;
int qiang=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>c[i][j];
if(c[i][j]=='S')
{
sx=i;
sy=j;//起点
}
else if(c[i][j]=='D')
{
ex=i;
ey=j;//终点
}
else if(c[i][j]=='X')
{
qiang++;//墙的数量
}
}
}
if(n*m-qiang<=t)//能走的步数比时间短 不可能在正好的时间到达
{
printf("NO\n");
continue;
}
flag=false;
c[sx][sy]='X';//起点不可能再走 变为墙
dfs(sx,sy,0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}