Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 53048 Accepted Submission(s): 14271
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
Author
ZHANG, Zheng
Source
Recommend
JGShining
#include<iostream>
using namespace std;
int sx,sy,dx,dy,m,n,t,flag;//起点坐标,终点坐标,列数,行数,时间,标志,孩子坐标
char map[10][10];//迷宫规模
int zou[4][2]={0,1,1,0,0,-1,-1,0};//方向,上,右,下,左
void dfs(int x,int y,int t)//深搜
{
if(flag==0) return ;
if(t==0)//时间已到
{
if(x==dx&&y==dy) {flag=0; return ;} //如果到达终点坐标,标志胜利
else
return ; //没达到终点,标志失败
}
if(t<(abs(dx-x)+abs(dy-y))) return ; //路径剪枝
if((t-(abs(dx-x)+abs(dy-y)))%2) return ;//奇偶剪枝
else
{
for(int i=0;i<4;i++)//四个方向判断
{
int nx=x+zou[i][0],ny=y+zou[i][1];//孩子坐标
if(map[nx][ny]=='.'||map[nx][ny]=='D')//孩子坐标为可走的路
{
map[nx][ny]='X';//已走过,不可再走
dfs(nx,ny,t-1);//检查孩子是否可行
map[nx][ny]='.';//回溯
}
}
}
return ;//遍历后结束
}
int main()
{
while(cin>>n>>m>>t)//输入行,列数,时间
{
if(n==0&&m==0&&t==0) break;//当行列时间皆为0退出
int i,j;char e;
for(i=0;i<n;i++)//循环行
{
for(j=0;j<m;j++)//循环列
{
cin>>e;map[i][j]=e;//输入字符,定义到整个二维数组中
if(e=='S'){sx=i;sy=j;}//标记开始
if(e=='D'){dx=i;dy=j;}//标记结束
}
}
flag=1;//初始化标志
dfs(sx,sy,t);//输入第一位置开始深搜
if(flag==1)
cout<<"NO"<<endl;//不可到达
else
cout<<"YES"<<endl;//可到达
}
return 0;
}