HDU 1010

题目:Tempter of the Bone


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

一、题目大意

小狗在一个古老的迷宫里发现了一根骨头,这使他非常着迷。然而,当他捡起它时,迷宫开始摇晃,小狗能感觉到地面在下沉。他意识到那块骨头是个陷阱,于是拼命想走出这个迷宫。
迷宫是一个大小为N×M的矩形。迷宫里有一扇门。一开始,门是关着的,它会在第T秒打开一小段时间(不到1秒)。因此,小狗必须正好在第T秒到达门口。在每一秒钟里,他都可以把一个街区移动到上、下、左、右相邻的街区之一。一旦他进入一个街区,这个街区的地面就会开始下沉,并在下一秒消失。他不能在一个街区停留超过一秒钟,也不能搬进一个参观过的街区。可怜的小狗能活下来吗?请帮帮他。

输入由多个测试用例组成。每个测试用例的第一行包含三个整数N、M和T(1<N、M<7;0<T<50),分别表示迷宫的大小和门打开的时间。接下来的N行给出了迷宫布局,每行包含M个字符。字符是下列字符之一:
“X”:一堵墙,小狗不能进去;
“S”:狗的起点;
“D”:门;或
“.”:空块。
输入以三个0终止。不处理此测试用例。

二、代码

搜索题,深搜即可,只是要记录步数,即题中的时间t

代码如下(示例):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

struct Node
{
    int x,y;
    int time;
};

char map[10][10];
int t;
bool flag;
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
//1.时间不能超过t
//2.flag>0时输出yes
//3.map[now.x][now.y]='D';(终点)
//4.X是墙不能走
//5.往四个方向找,时间每次+1,如果map[now.x][now.y]='.',将map[now.x][now.y]='X',继续Dfs(next)
void Dfs(Node now)
{
Node next;
if(flag) return ;
if(now.time>t) return ;
if(now.time==t)
{
    if(map[now.x][now.y]=='D')
    {
        flag=1;
    }
    return ;
}
for(int i=0;i<4;i++)
{
    next.x=now.x+dir[i][0];
    next.y=now.y+dir[i][1];
    next.time=now.time+1;
    if(map[next.x][next.y]!='X')
    {
        if(map[next.x][next.y]=='.')
        {
            map[next.x][next.y]='X';
            Dfs(next);
            map[next.x][next.y]='.';
        }
        else
        {
            Dfs(next);
        }
    }
}
}

int main()
{
    int n,m,p;
    int i,j,x,y;
    Node now;
    while(scanf("%d%d%d",&n,&m,&t))
    {
        if(n == 0 && m == 0 && t == 0)
        {
            break;
        }
        memset(map,'X',sizeof(map));  //初始化都为墙
        flag = 0;
        for(i = 1; i <= n; i++)
        {
            for(j = 1; j <= m; j++)
            {
                cin >> map[i][j];
                if(map[i][j] == 'S')//起点
                {
                    now.x = i;
                    now.y = j;
                }
                if(map[i][j] == 'D')//出口
                {
                    x = i;
                    y = j;
                }
            }
        }
        now.time = 0;
        p = abs(now.x-x) + abs(now.y-y);
        map[now.x][now.y] = 'X';

        if(p%2 == t%2)    //时间和需要走的步骤都为奇数或偶数则搜索(奇偶剪枝)
        {
            Dfs(now);
        }

        if(flag > 0)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值