HDU 1072 Nightmare

Nightmare

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes. 

Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1. 

Here are some rules: 
1. We can assume the labyrinth is a 2 array. 
2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too. 
3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth. 
4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb. 
5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish. 
6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6. 
 

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth. 
There are five integers which indicate the different type of area in the labyrinth: 
0: The area is a wall, Ignatius should not walk on it. 
1: The area contains nothing, Ignatius can walk on it. 
2: Ignatius' start position, Ignatius starts his escape from this position. 
3: The exit of the labyrinth, Ignatius' target position. 
4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas. 
 

Output

For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1. 
 

Sample Input

    
    
3
3 3
2 1 1
1 1 0
4 8
1 1 3
2 1 1 0 1 1 1 0
1 0 4 1 1 0 4 1
1 1 1 4 1 1 1 3
1 0 0 0 0 0 0 1
5 8
1 0 0 0 1 0 0 1
1 2 1 1 1 1 1 4 1 4 1 0 1 1 0 1
1 1 4 1 1 1 1 1
1 0 0 0 0 3 0 1
 

Sample Output

    
    
4
-1
13

题意:在n * m 的迷宫里,Ignatius身上有一颗定时炸弹,炸弹最初设置时间为6分钟,他只能在其位置的上,下,左,右移动,每移动一次,花费一分钟。问他是否能走出迷宫?其中0代表墙,1代表空地,2代表他的起始位置,3代表出口,4代表重置。当人走到4炸弹的时间不为0时,可以重新设置时间为6;当人走到3炸弹时间大于0时,表示他成功走出迷宫。注:每个位置可以重复访问
分析:虽然每个位置是可以重复走,但是4访问一次就够了,第一次访问4时,炸弹时间会被重置为最大,当再次访问时,虽然时间又被重置为最大,但已走的步数肯定增加了,所以访问过4之后,直接标记为0,无需再访问。采用记忆化搜索
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;
int n,m;
int maze[10][10];//迷宫
int sx,sy;//起始坐标
//搜索方向
int dx[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};

struct pp
{
    int x,y;
    int cont;
    int move;
};

void bfs()
{
    queue<pp> que;
    pp cur;
    cur.x = sx;
    cur.y = sy;
    cur.cont = 6;//设置初始时间
    cur.move = 0;//移动步数为0
    que.push(cur);//将起点加入队列
    maze[sx][sy] = 0;//标记,不再访问
    while(que.size())
    {
        cur = que.front();
        que.pop();
        pp next;
        for(int i = 0;i < 4;i++) //对四个方向进行搜索
        {
            int nx = cur.x + dx[i][0];
            int ny = cur.y + dx[i][1];
            int nz = cur.cont;
            //判断是否可以移动以及是否被访问
            if(nx >= 0 && nx < n && ny >= 0 && ny < m && maze[nx][ny] != 0)
            {
                if(maze[nx][ny] == 1)//若移动后的位置为1
                {
                    nz--; //判断在此位置的时间是否为0
                    if(nz >= 1) //若大于0,将步数在cur的基础上加1,入队
                    {
                        next.x = nx;
                        next.y = ny;
                        next.cont = nz;
                        next.move = cur.move + 1;
                        que.push(next);
                    }
                }
                if(maze[nx][ny] == 4)//若移动后的位置为4
                {
                    nz--;//判断在此位置的时间是否为0
                    if(nz >= 1)//若大于0,将时间重置为6,步数在cur的基础上加1,入队
                    {
                        next.x = nx;
                        next.y = ny;
                        next.cont = 6;
                        next.move = cur.move + 1;
                        que.push(next);
                    }
                    maze[nx][ny] = 0;//标记,不再访问
                }
                if(maze[nx][ny] == 3 && cur.cont > 1)//若到达终点且时间大于1,输出最小步数
                {
                    cur.move++;
                    printf("%d\n",cur.move);
                    return ;
                }
            }
        }
    }
    printf("-1\n");//若无法到达终点,输出-1
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < m;j++)
            {
                scanf("%d",&maze[i][j]);
                if(maze[i][j] == 2)//记录起始位置
                {
                    sx = i;
                    sy = j;
                }
            }
        }
        bfs();
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值