Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 54289 Accepted Submission(s): 14618
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
典型的迷宫搜索,做出该题将具有里程碑式的意义!
能不能枚举出所有抵达方案,再在通过检查时间时间是否吻合,得到结果,用
DFS
进行搜索。
DFS
搜索完成后,发现超时,得剪枝才行。
每个
block
只能走一次
要求恰好某个给定的时间到达出口
如果可走的
block
的总数小于时间,将会产生什么情况?
可以把
map
看成这样:
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
从为
0
的格子走一步,必然走向为
1
的格子
从为
1
的格子走一步,必然走向为
0
的格子
即:
0 ->1
或
1->0
必然是奇数步
0->0
走
1->1
必然是偶数步
结论:
所以当遇到从0走向 0但是要求时间是奇数的,或者,从 1 走向 0 但是要求时间是偶数的都可以直接判断不可达!
那么设所在位置
(
x,y
)
与 目标位置
(
dx,dy
)
如果
abs(dx-x)+abs(
dy
-y)
为偶数,则说明
abs(dx-x)+
和
abs(
dy
-y)
的奇偶性相同,需要走偶数步
如果
abs(dx-x)+abs(
dy
-y)
为奇数,那么说明
abs(dx-x)
和
abs(
dy
-y)
的奇偶性不同,需要走奇数步
解为
abs(dx-
sx
)+abs(
dy-sy
)
的奇偶性就确定了所需要的步数的奇偶性!!
而
(
ti-setp
)
表示剩下还需要走的步数,由于题目要求要在
ti
时恰好到达,那么
(
ti
-step)
与
abs(x-y)+abs(dx-
dy
)
的奇偶性必须相同
因此
temp=
ti
-step-abs(dx-x)-abs(
dy
-y)
必然为偶数!
code:
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
int dir[4][2]={0,1,1,0,-1,0,0,-1};
int vis[6][6];
char map[6][6];
int sx,sy,dx,dy;
int m,n,T;
bool dfs(int x,int y,int step)
{
if(map[x][y]=='D'&&step==T)
{
return true;
}
int temp;
temp=T-step-abs(x-dx)-abs(y-dy);
if(temp<0||temp%2==1) return false;
for(int i=0;i<4;i++)
{
int _x,_y;
_x=x+dir[i][0];
_y=y+dir[i][1];
if(_x<m&&_y<n&&_x>=0&&_y>=0&&vis[_x][_y]==0&&map[_x][_y]!='X')
{
vis[_x][_y]=1;
if(dfs(_x,_y,step+1)) return true;
vis[_x][_y]=0;
}
}
return false;
}
int main()
{
while(cin>>m>>n>>T)
{
if(m==0&&n==0&&T==0) break;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
cin>>map[i][j];
if(map[i][j]=='S')
{
sx=i;sy=j;
}
if(map[i][j]=='D')
{
dx=i;dy=j;
}
vis[i][j]=0;
}
vis[sx][sy]=1;
if(dfs(sx,sy,0)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}