Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 104767 Accepted Submission(s): 28451
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
Author
ZHANG, Zheng
Source
ZJCPC2004
Recommend
JGShining | We have carefully selected several similar problems for you: 1241 1242 1072 1312 1026
分析:
题意:一只狗开始时在迷宫里面S处,出口为D处。从它拿到骨头后迷宫开始塌陷,狗每一秒可以向上下左右是个方向移动,狗到达的位子在下一秒就会塌陷,门会在T秒是打开。问狗可以逃出去吗?
这一题因为要考虑所有的可能所以用DFS。
题目要注意两处剪枝:
(1):如果”.”的数目比T小,则不可能。
(2):因为狗只能横向和纵向移动,所以狗从当前位子直接到达D,需要fabs(di-si)+fabs(dj-sj)。若果不是直接到达的,那狗在横向或纵向多走的步数一定是偶数的(一来一回)。所以若果从当前位子可以在第T秒到达D则一定有:T-cnt-fabs(si-di)-fabs(sj-dj)为偶数且大于等于0.
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
char m[10][10];
int N,M,T,di,dj,si,sj,escape=0;
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
void dfs(int si,int sj,int cnt)
{
if(si<0 || sj<0 || si>=N || sj>=M)
return;
if(si==di && sj==dj && cnt==T)
{
escape=1;
return;
}
int temp;
temp = T-cnt-fabs(si-di)-fabs(sj-dj);
if(temp<0 || temp%2)
return;
for(int i=0;i<4;i++)
{
if(m[si+dir[i][0]][sj+dir[i][1]]!='X')//=='.' || =='D'
{
m[si+dir[i][0]][sj+dir[i][1]]='X';
dfs(si+dir[i][0],sj+dir[i][1],cnt+1);
if(escape) return;
m[si+dir[i][0]][sj+dir[i][1]]='.';
}
}
return;
}
int main()
{
while(scanf("%d%d%d",&N,&M,&T)!=EOF)
{
if(N==0 && M==0 && T==0) break;
int w = 0;
getchar();
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
scanf("%c",&m[i][j]);
if(m[i][j]=='S')
{
si = i;
sj = j;
}
else if(m[i][j]=='D')
{
di = i;
dj = j;
}
else if(m[i][j]=='X')
w++;
}
getchar();
}
if(N*M-w<T)
{
printf("NO\n");
continue;
}
escape = 0;
m[si][sj]='X';
dfs(si,sj,0);
if(escape)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}