ZOJ 2110 Tempter of the Bone

来源:互联网 发布:网络中继是什么意思 编辑:程序博客网 时间:2024/06/08 01:33


Tempter of the Bone

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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



普通的搜索题,写完以后不幸TLE

之后看题解,发现这道题作为一道教程(完全没看)题,专门讲剪枝…… WTF!

于是加了个剪枝过了。300+ms,实际上可以各种花式剪枝把时间压到80ms,但是看写博客的时间就知道我为什么不想多写


#include<cstdio>#include<iostream>#include<cmath>#include<cstring>#include<algorithm>using namespace std;char map[20][20];int mx[5]={0,-1,0,1,0},my[5]={0,0,1,0,-1};int n,m,Time;int tx,ty;int flag=0;void dfs(int x,int y,int t){if(x==tx && y==ty && t==Time){flag=1;return;}if(map[x][y]=='X')return;//if(t>=Time)return;//剪枝 int temp=(Time-t)-fabs(x-tx)-fabs(y-ty);if(temp<0 ||temp %2)return;//int i;for(i=1;i<=4;i++){int nx=x+mx[i];int ny=y+my[i];if(nx>=0 && nx<n && ny>=0 && ny<m)//if(map[nx][ny]!='X'){map[x][y]='X';dfs(nx,ny,t+1);if(flag)return;map[x][y]='.';}}return;}int main(){while(scanf("%d%d%d",&n,&m,&Time)!=EOF){flag=0;if(n==0 && m==0 && Time==0)break;int i,j;int xx,yy;for(i=0;i<n;i++){scanf("%s",map[i]);for(j=0;j<m;j++){  if(map[i][j]=='S')xx=i,yy=j;  if(map[i][j]=='D')tx=i,ty=j;//目标点 }}dfs(xx,yy,0);if(flag==1)printf("YES\n");else printf("NO\n");}return 0;}






0 0
原创粉丝点击