HDU1010 Tempter of the Bone

来源:互联网 发布:网络日语培训班 编辑:程序博客网 时间:2024/06/16 01:20

HDU1010 Tempter of the Bone

题目描述
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时刻到达出口,每个点不能重复走。

思路
dfs,注意剪枝,不然会超时。这题还有一个比较恶心的地方,测试数据可能会有多余的空格或换行,所以要么用c++里的cin来读,或者用scanf(“%s”)一行一行的读,不然也会wa。

下面是代码

#include <iostream> #include <cstdio>#include <cstdlib>#include <cmath>#include <fstream>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <queue>#include <stack>#include <vector>#include <list>#include<sstream>#include<ctime>using namespace std;int dix[4]={0,1,0,-1};int diy[4]={1,0,-1,0};int n,m,t,flag,sx,sy,ex,ey;char map[10][10];int f[10][10];      //记录该点是否走过void dfs(int x,int y,int time)  {    if(flag==1||time>t) //如果flag为1或者用的时间已经超过t,结束dfs    return;    if(x==ex&&y==ey&&time==t) // 如果恰好在t时到达出口    {        flag=1;              //将flag标记为1,结束dfs        return;                         }     //奇偶剪枝    //比如你现在在点p(x1,y1)处,出口(x2,y2)     //那么你想在剩下T时间恰好走到出口需要满足T的奇偶性与abs(x1-x2)+abs(y1-y2)的奇偶性相同    //如果不同则不可能。    int mindis=abs(x-ex)+abs(y-ey);       if((mindis+ t-time )%2!=0)        return;        for(int i=0;i<4;i++)       {        int x1=x+dix[i];        int y1=y+diy[i];        if(x1>=1&&x1<=n&&y1>=1&&y1<=m&&map[x1][y1]!='X'&&f[x1][y1]==0)//如果满足条件        {            f[x1][y1]=1;   //将这个点标记为1,表示走了这个地方            dfs(x1,y1,time+1);               f[x1][y1]=0;   //如果不走这个点         }     }}int main()  {     int num;   while(1)   {       num=0;       scanf("%d %d %d",&n,&m,&t);       getchar();                  //吃掉回车       if(!(n+m+t)) return 0;       for(int i=1;i<=n;i++)       for(int j=1;j<=m;j++)       {          cin>>map[i][j];          if(map[i][j]=='X')  // 记录障碍的个数          num++;          if(map[i][j]=='S')  //记录起点坐标          {            sx=i;            sy=j;          }          if(map[i][j]=='D') //记录出口坐标          {            ex=i;            ey=j;          }       }       if(n*m-num-1<t)    //如果可走的点比时间t少,则不可能恰好在t时走到出口       {         printf("NO\n");         continue;       }       flag=0;       memset(f,0,sizeof(f));   //初始化       f[sx][sy]=1;       //将起点标记为1       dfs(sx,sy,0);      //dfs       if(flag==1)       printf("YES\n");       else       printf("NO\n");    }     return 0;}
0 0
原创粉丝点击