HDOJ 1010 Tempter of the Bone

来源:互联网 发布:南京高新沿江网络问政 编辑:程序博客网 时间:2024/06/06 01:08

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 106722    Accepted Submission(s): 29024


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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
 

Sample Output
NOYES
 

Author
ZHANG, Zheng
 

Source
ZJCPC2004
 


读完题之后会发现这个题没法用bfs,只能用dfs。然后直接用dfs会TLE……

所以要剪枝。这题剪枝的方法是奇偶剪枝,百度一下,百度百科里面讲的非常明白,我就不详细分析了。


题意:doggie被困在一个地方了,现在要跑。离开这个地方的门D会在第T秒的时候开启,并且只持续开启1秒。

doggie本人跑路的时候脚下踩着砖块,这些砖块会在他踩上的时候慢慢坏掉,如果砖块完全坏掉他还没有离开这块砖的话,他就被困住了。所以每块砖他都只能踩1秒。问:doggie能不能成功离开这里?


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n,m,t,sx,sy,ex,ey,Flag,num,time;char map[10][10];int vis[10][10];int dis[4][2]={{0,1},{0,-1},{1,0},{-1,0}};void dfs(int x,int y,int time){if(Flag==1) return;if(x==ex&&y==ey&&time==t){Flag=1;return;}int temp=abs(x-ex)+abs(y-ey);if(temp>t-time||(temp+t-time)%2==1) return;for(int i=0;i<4;i++){int tx=x+dis[i][0];int ty=y+dis[i][1];if(tx>=0&&ty>=0&&tx<n&&ty<m&&vis[tx][ty]==0&&map[tx][ty]!='X'){vis[tx][ty]=1;dfs(tx,ty,time+1);vis[tx][ty]=0;}}}int main(){int i,j;while(scanf("%d%d%d",&n,&m,&t),n||m||t){num=Flag=0;memset(vis,0,sizeof(vis));for(i=0;i<n;i++){scanf("%s",map[i]);for(j=0;j<m;j++){if(map[i][j]=='S'){sx=i;sy=j;}if(map[i][j]=='D'){ex=i;ey=j;}if(map[i][j]=='X')num++;}}if(n*m-num-1<t)printf("NO\n");else{vis[sx][sy]=1;dfs(sx,sy,0);if(Flag)printf("YES\n");elseprintf("NO\n");}}return 0;}

0 0
原创粉丝点击