【杭电】[1010]Tempter of the Bone
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
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 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
Sample Output
NO YES
给出一幅地图 问能不能在第T秒到达出口 因为并不是找最短距离,所以需要多次递归 而因为递归次数巨大 所以需要进行剪枝 比较好的剪枝思路有
- 如果已经有满足条件的,则其余递归都可以停止
- 记录总可走'.'如果tcnt+1<T则肯定不行
- 最重要的根据曼哈顿距离(从一个点到达另外一个点的最短路径长度|x1-x2|+|y1-y2|)进行剪枝,路径长度(非最短)与最短路径的长度同奇偶,它们的差一定是偶数。所以判断当前位置到终点的曼哈顿距离与它到终点如果成立所需要的路径长度的奇偶性,如果它们的差是奇数则可以停止递归。
#include<stdio.h> int move[4]= {1,-1,0,0}; char map[10][10]; int cnt[10][10]; int H,W,T; int sH,sW,eH,eW,se; bool win; int abs(int m) { return m>0?m:-m; } void dfs(int n,int m) { if(win) return ; int t=(T-cnt[n][m])-(abs(n-eH)+abs(m-eW)); if(t<0||t&1) return ; if(n==eH&&m==eW&&cnt[n][m]==T) win=true; for(int i=0; i<4; i++) { int tn=n+move[i],tm=m+move[(i+2)%4]; if(tn>=0&&tn<H&&tm>=0&&tm<W&&map[tn][tm]!='X'&&cnt[tn][tm]>cnt[n][m]+1&&cnt[n][m]<T) { cnt[tn][tm]=cnt[n][m]+1; dfs(tn,tm); cnt[tn][tm]=99999999; } } } int main() { while(scanf("%d %d %d",&H,&W,&T),H||W||T) { getchar(); int tcnt=0; for(int i=0; i<H; i++) { for(int j=0; j<W; j++) { cnt[i][j]=99999999; map[i][j]=getchar(); if(map[i][j]=='S') sH=i,sW=j; else if(map[i][j]=='D') eH=i,eW=j; else if(map[i][j]=='.') tcnt++; } getchar(); } if(tcnt+1<T) { printf("NO\n"); continue; } se=abs(sH-eH)+abs(sW-eW); win=false; cnt[sH][sW]=0; dfs(sH,sW); printf("%s\n",win?"YES":"NO"); } return 0; }
题目地址:【杭电】[1010]Tempter of the Bone
查看原文:http://www.boiltask.com/blog/?p=1925