Tempter of the Bone
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 15 Accepted Submission(s) : 9
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
'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
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES
Author
Source
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int dr[][]={{,},{,},{-,},{,-}};
int f[][];
char mp[][];
int sx,sy,tx,ty,t,n,m,i,j;
int check(int x,int y)
{
if (x>= && x<n && y>= && y<m && mp[x][y]!='X' && !f[x][y]) return ;
else return ;
}
int dfs(int x,int y,int time)
{
if (time== && x==tx && y==ty)
return ;
for(int i=;i<;i++)
{
int xx=x+dr[i][];
int yy=y+dr[i][];
if (check(xx,yy))
{
f[xx][yy]=;
if (dfs(xx,yy,time-)) return ;
f[xx][yy]=;
}
}
return ;
} int main()
{
while(scanf("%d%d%d",&n,&m,&t) && n!=)
{
for(i=;i<n;i++)
{
scanf("%s",&mp[i]);
for(j=;j<m;j++)
if (mp[i][j]=='S') sx=i,sy=j;
else if (mp[i][j]=='D') tx=i,ty=j;
}
if (t==)
{
if (sx!=tx && sy!=ty) printf("NO\n");
else if (sx==tx && sy==ty) printf("YES\n");
continue;
}
if (abs(tx-sx)+abs(ty-sy)>t || (t-abs(tx-sx)-abs(ty-sy))%!=) {printf("NO\n");continue;} memset(f,,sizeof(f));
f[sx][sy]=;
if (dfs(sx,sy,t)) printf("YES\n");
else printf("NO\n");
} return ;
}