FOSU菜鸟新篇章Tempter of the Bone

来源:互联网 发布:php session读取 编辑:程序博客网 时间:2024/05/15 03:01

Tempter of the Bone

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


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题目链接点击打开链接这道题的意思是小狗从S点出发走到D点,每次走一步而且只能是相邻的,X代表不可走,.代表可走,问在T步内可不可以到达终点D,这道题可以用深搜解决,不过深搜很容易超时,要加上一定的剪枝才可以不说了,看代码解释
#include<stdio.h>#include<iostream>#include<math.h>#include<cstring>using namespace std;char map[10][10];int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};int s1,s2,e1,e2,N,M,T,flag;int dfs(int a,int b,int step1){if(step1==T&&a==e1&&b==e2)//判断是否到达终点,是否步数为T,是就返回递归return 1;if(T-step1<abs(a-e1)+abs(b-e2) || (T-step1-abs(a-e1)-abs(b-e2))%2)return 0;//这里是一个小的剪枝,当(a,b)点到达终点(e1,e2)的最小步数大//于规定步数(T)-走过的步数(step1)就退出递归,T-step1代表剩下可走的步数,而abs(a-e1)+abs(b-e2)当前走//到的点与目的点最短距离,如果它要绕路就必须得是偶数for(int i=0;i<4;i++)//每次找到相临的点,即该点的上下左右符合要求的点,根据上面的dir[][]方向数组设置{if(map[a+dir[i][0]][b+dir[i][1]]!='X')//判断地图是否可以走{map[a+dir[i][0]][b+dir[i][1]]='X';//将走过的地图标记为X(不可走)if(dfs(a+dir[i][0],b+dir[i][1],step1+1))return 1;//这里的剪枝很重要,就是T步内已找到目的(e1,e2)后就返回为1,可以理解为结束递归map[a+dir[i][0]][b+dir[i][1]]='.';//如果没在规定的T步内找到(e1,e2)就将地图上走过的点并标记为X的点还原回来}}return 0;}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int i,j;while(scanf("%d%d%d",&M,&N,&T)&&M+N+T!=0){memset(map,'X',sizeof(map));//每次都将地图初始化为X,//这样做的好处就是不用判断小狗是否到达了边界,这样减少了以后的操作for(i=1;i<=M;i++){//scanf("%s",map[i]+1);for(j=1;j<=N;j++){cin>>map[i][j];//如果用scanf会发现超时,应该是输入的问题if(map[i][j]=='S')//找到开始的坐标跟结束的坐标{s1=i;s2=j;}if(map[i][j]=='D'){e1=i;e2=j;}}}map[s1][s2] = 'X';if(dfs(s1,s2,0))printf("YES\n");elseprintf("NO\n");}return 0;}


1 0
原创粉丝点击