Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 85142 Accepted Submission(s): 23196
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
今天晚上是栽到这道题上了,一看题第一反应就是BFS,然后也这样做了。但是由于题目理解没理解好,提交一遍错一遍,但是一直是BFS,后来想了想得用深搜。于是又写了深搜,超时,给跪了。改,还超,再改,还超,真受不了。看解析,奇偶剪枝,第一次知道这个,很妙。提交过了,时间大大的减少
奇偶剪枝 :就是从一点开始到终点你判断一下一共需要走的步数是奇数还是偶数,如果和在规定步数内要求的不一样 肯定就要排除了
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
char map[10][10];
int vis[10][10];
int n,m,t;
int sx,sy;
int ex,ey;
int flag;
int step;
int gx[4]={0,1,0,-1};
int gy[4]={1,0,-1,0};
void dfs(int x,int y,int st)
{
if(x==ex&&y==ey&&st==t)
{
flag=1;
printf("YES\n");
}
if(flag||st>t) return;
if(((abs(ex-x)+abs(ey-y))%2)!=(abs(t-st)%2))
return ;
for(int i=0;i<4;i++)
{
int fx=x+gx[i];
int fy=y+gy[i];
if(fy>0&&fy<=m&&fx>0&&fx<=n&&map[fx][fy]!='X'&&vis[fx][fy]==0)
{
vis[fx][fy]=1;
dfs(fx,fy,st+1);
if(flag) break;
vis[fx][fy]=0;
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
if(n==0&&m==0&&t==0) break;
getchar();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='S')
{
sx=i;
sy=j;
}
if(map[i][j]=='D')
{
ex=i;
ey=j;
}
}
getchar();
}
memset(vis,0,sizeof(vis));
vis[sx][sy]=1;
step=0;
flag=0;
dfs(sx,sy,step);
if(flag==0)
printf("NO\n");
}
return 0;
}