Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 71012 Accepted Submission(s): 19566
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
题目大意就是规定要在第几秒的时候走到某个固定的位置(不能重复走),问是否可以,如果可以,输出YES,否则NO。其实就是把经过这么多秒能走到的位置都进行一次判断,如果用BFS,因为不能重复走,所以判断一个位置是否被该步之前的步数走到过就是很必要。以前的BFS都是最短的步数或者时间,所以直接判断是否走过就行,因为如果已经被走过,说明肯定有比它更快的方法到达过此点。但此题如果用这种思想则不行。在该题中,通过其他路径走到该点,该点仍可以被另外的途径走过。所以直接判断是否走过则不行。
需要DFS的回溯,
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
//ios::sync_with_stdio(false);
//ios::sync_with_stdio(false);
const int maxm=10;
int vis[maxm][maxm],t,T,n,m;
char mat[maxm][maxm];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int flag;
struct node {
int x;
int y;
int step;
}s,ta;
int judge(int x,int y)
{
if(x>0&&x<=n&&y>0&&y<=m&&mat[x][y]=='.')
return 1;
else
return 0;
}
void dfs(int x,int y,int t)
{
int i,xx,yy;
int tmp=T-t-abs(ta.x-x)-abs(ta.y-y);
if(flag==1)
return ; <span style="white-space:pre"> </span>//当其为1的时候就说明有解,那样的话,让其他仍在搜索的点返回
if(t==T)
{
if(xx==ta.x&&yy==ta.y) //在T秒刚好到达
{flag=1;return ;}
}
if(tmp<0||tmp&1) //和1与相当于%2;效率快多了
// if(tmp<0||tmp%2!=0) //剪枝
return ;
for(i=0;i<4;i++)
{
xx=x+dx[i];
yy=y+dy[i];
if(judge(xx,yy))
{
mat[xx][yy]='X';
dfs(xx,yy,t+1);
mat[xx][yy]='.';//回溯,
}
}
return ;
}
int main()
{
int i,j,wall;
while(scanf("%d%d%d",&n,&m,&T)&&n&&m&&T)
{
flag=0;wall=0;
//memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
{scanf("%s",mat[i]+1);
for(j=1;j<=m;j++)
{if(mat[i][j]=='D')
{
ta.x=i;
ta.y=j;
mat[i][j]='.';
}
if(mat[i][j]=='S') {s.x=i;s.y=j;}
if(mat[i][j]=='X') wall++;
}
}
if(n*m-wall<T) //剪枝,如果可走的位置比T少,肯定不行
printf("NO\n");
else {
dfs(s.x,s.y,0);
if(flag>0)
printf("YES\n");
else
printf("NO\n");
}
}
}