Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 91286 Accepted Submission(s): 24824
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
解析:注意,是T s恰好到达门那里,并不是 T s内。
1.01剪枝。
在方格中从一个点到另一个点,无论你怎样走,总步数的奇偶性总是不变的,比如在9X9的方格中,从(1,1)到(2,2)的最短距离为2,那么无论你怎样走,从(1,1)到(2,2)的步数都是一个偶数。
由上即得到第一个剪枝:设起点(sx,sy),终点(zx,zy),若abs(sx-zx)+abs(sy-zy)与T的奇偶性不一致,则NO;
2.若点钱搜索到点(x,y
),已走step步,若step+abs(x-zx)+abs(y-zy)>T则返回0;
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
int n,m,t,sx,sy,zx,zy;
int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
char s[10][10];
bool used[10][10];
bool dfs(int x,int y,int step)
{
if(s[x][y]=='D')return step==t;
if(step+abs(x-zx)+abs(y-zy)>t)return 0;
int i,j,k;
for(k=0;k<4;k++)
{
i=x+dx[k],j=y+dy[k];
if(i<0 || i>=n || j<0 || j>=m ||
used[i][j] || s[i][j]=='X')continue;
used[i][j]=1;
if(dfs(i,j,step+1))return 1;
used[i][j]=0;
}
return 0;
}
int main()
{
int i,j,k;
while(scanf("%d%d%d",&n,&m,&t),n!=0)
{
for(i=0;i<n;i++)scanf("%s",s[i]);
for(k=0,i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(s[i][j]=='S')sx=i,sy=j;
if(s[i][j]=='D')zx=i,zy=j;
if(s[i][j]=='X')k++;
}
if(n*m-k<t || (abs(zx-sx)+abs(zy-sy)+t)&1)goto d1;
memset(used,0,sizeof(used));
used[sx][sy]=1;
if(dfs(sx,sy,0))printf("YES\n");
else d1:printf("NO\n");
}
return 0;
}