HDU 1010 View Code

Tempter of the Bone

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


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
 

Author
ZHANG, Zheng
 

Source
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1072  1026  1240  1175  1015 

思路:深搜加剪枝;这道题好坑,我用广搜没出来,深搜各种wa or 超时,深刻认识到了剪枝的重要性。废话不多说了。上代码。边写思路;
#include <iostream>                              //头文件太多。。有的没有用。懒得弄了;
#include <cstdio>
#include<math.h>
using namespace std;
#include<math.h>
char map[201][201];                              //存地图
int  vis[201][201];                              //标记;
int A,B,T,prove;                                 //边界AB时间T,prove用来判断是否可以到达;
int ex,ey,tx,ty;                                 //出发点S,末点D;
int d[4][2]={0,1,1,0,-1,0,0,-1};                 //移动的四个方向;
void DFS(int x, int y,int step)                  //深搜开始:
{
    if(prove==1)                                    //如果能够达到,prove=1;
        return;                                      //返回;
     if(step==T)                                           //如果步数达到T;
     {
         if(x==tx&&y==ty)                            //判断此时的x y坐标是否为终点坐标;
             prove=1;
         return;
     }
     int dis = abs(x - tx) + abs(y - ty);                   //剪枝:
     if((dis > T - step) || ((dis + T - step) % 2 != 0))       如果 当前到终点的最短路径大于剩下的步数,可定不可能达到终点;
         return;
     int i;
     for(i=0;i<4;i++)                                                       //扩展
     {
         int x1=x+d[i][0];
         int y1=y+d[i][1];
         if(x1<A&&x1>=0&&y1>=0&&y1<B&&map[x1][y1]!='X'&&vis[x1][y1]==0)     判断地图边界是否超过,是否用过该点;
         {
             vis[x1][y1]=1;
             DFS(x1,y1,step+1);
             vis[x1][y1]=0;                                             //撤销该点标记;
         }
     }
}
int main()
{
    while(scanf("%d%d%d",&A,&B,&T)!=EOF)                               //输入行列和最大的时间;
    {
        if(A==0&&B==0&&T==0)
            break;
        int i,j;
        getchar();
        int sum=0;
        for(i = 0; i < A; i++)                                         //输入地图
        {
           for(j = 0; j< B; j++)
           {
               scanf("%c", &map[i][j]);
               vis[i][j]=0;
               if(map[i][j]=='S')                                     //找出出发点坐标;
               {
                   ex=i; 
                   ey=j;
                   vis[i][j]=1;
               }
               if(map[i][j]=='D')                                 //找出终点坐标;
               {
                   tx=i;
                   ty=j;
               }
               if(map[i][j]=='X')                                 //计算地图中X的个数;
                   sum++;
           }
           getchar();                                           //吸收换行符;
        }
        if(A * B - sum - 1 < T)                              //如果地图中能够走的点小于T,可定到达不到终点,动动手指头就知道了。
        {
           printf("NO\n");
            continue;
       }
        prove =0;
        DFS(ex,ey,0);
        if(prove==0)
           printf("NO\n");                       //输出结果;
        else
           printf("YES\n");
        
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值