hdu 1010

来源:互联网 发布:jquery.poshytip.js 编辑:程序博客网 时间:2024/06/14 03:44

Tempter of the Bone(DFS)

Tempter of the Bone

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


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
 
题意:能否恰好在T时刻从S到D。

题解:DFS+奇偶剪枝

剪枝:

1.当剩余时间小于剩余步数时,一定不能达到。

2.奇偶剪枝


意思是剩下的时间与步数同为奇数或同为偶数时,才有可能完成。

#include<cstdio>#include<cstring>#include<cmath>using namespace std;int n,m,t;char map[11][11];int wall,xx,yy,ex,ey;int dx[4] = {1,-1,0,0};int dy[4] = {0,0,1,-1};int flag;void dfs(int xi,int yi,int cnt){int i,tem;if (xi<0||xi>=n||yi<0||yi>=m) return ;//出界 if (cnt==t&&xi==ex&&yi==ey){//到达 flag = 1;//return ;不写下面的if,这样会超时 1000-->202                                                         }if (flag)//立即结束,否则会超时 return ;int s1=ex-xi;int s2=ey-yi;if (s1<0) s1=-s1;if (s2<0) s2=-s2;tem =t-cnt- s1-s2;if (tem<0||tem&1) return ;for (i=0;i<4;i++){if (map[xi+dx[i]][yi+dy[i]]!='X'){map[xi+dx[i]][yi+dy[i]]='X';dfs(xi+dx[i],yi+dy[i],cnt+1);map[xi+dx[i]][yi+dy[i]]='.';}}return ;}int main(){int i,j;while (scanf ("%d %d %d",&n,&m,&t)!=EOF){if (n==0 &&m==0 &&t==0) break;wall=0;for (i=0;i<n;i++){getchar();scanf ("%s",&map[i]);for (j=0;j<m;j++){if (map[i][j]=='S')xx=i,yy=j;if (map[i][j]=='D')ex=i,ey=j;if (map[i][j]=='X')wall++;}}if (n*m-wall<=t){printf ("NO\n");continue;}flag = 0;map[xx][yy]='X';dfs(xx,yy,0);if (flag) printf ("YES\n");elseprintf ("NO\n");}return 0;}


0 0
原创粉丝点击