hdu 1072 Nightmare

来源:互联网 发布:无极郭敬明知乎 编辑:程序博客网 时间:2024/06/10 02:39

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

33 32 1 11 1 01 1 34 82 1 1 0 1 1 1 01 0 4 1 1 0 4 11 0 0 0 0 0 0 11 1 1 4 1 1 1 35 81 2 1 1 1 1 1 4 1 0 0 0 1 0 0 1 1 4 1 0 1 1 0 1 1 0 0 0 0 3 0 1 1 1 4 1 1 1 1 1 

Sample Output

 4-113 

【题意】现在你身上有个炸弹,炸弹呢很变态,你不能从身上除掉,现在你在一个迷宫中,炸弹定时六秒,六秒后就会爆炸,虽然情况紧急,但是每秒钟你只能上下左右移动一格,现在迷宫中有一些刷新点,可以把定时刷新成六秒,刷新不消耗时间,只有移动耗时,迷宫中只有一个终止点,你需要飞奔到终止点并且炸弹没有爆炸(定时还剩至少一秒)。现在问你到终止点的最短时间,如果还没有到达终止点就挂了,那就输出-1.

【分析】记录起始终止点,然后就bfs就行,但是注意一个点被搜索到两次的条件,开始以为是总时间小于当前就行,后来发现是当前炸弹的时间大于以前的时间。(bfs不习惯用的我求大佬别喷-_-!)

【代码】

#include <bits/stdc++.h>using namespace std;int len_x,len_y;int s_x,s_y,e_x,e_y;int mat[10][10];struct P{    int x,y,time,sum_time;};int sum_time[10][10];//表示到达该点的最短时间int tim[10][10];//表示到达该点炸弹剩余的最长时间int changex[]= {0,0,1,-1};int changey[]= {1,-1,0,0};bool in_mat(int x,int y)//判断是否在图中{    if(x<0||x>=len_x)        return false;    if(y<0||y>=len_y)        return false;    return true;}int main(){//    freopen("in.txt","r",stdin);    int T;    scanf("%d",&T);    while(T--)    {        memset(tim,0,sizeof(tim));        memset(sum_time,0x3f,sizeof(sum_time));        scanf("%d%d",&len_x,&len_y);        for(int i=0; i<len_x; i++) //完成输入,统计出起点终点            for(int j=0; j<len_y; j++)            {                scanf("%d",&mat[i][j]);                if(mat[i][j]==2)                {                    s_x=i;                    s_y=j;                }                else if(mat[i][j]==3)                {                    e_x=i;                    e_y=j;                }            }        queue<P>x;        P a,b;        a.sum_time=0;        a.time=6;        a.x=s_x;        a.y=s_y;        tim[a.x][a.y]=6;        sum_time[a.x][a.y]=0;        x.push(a);        while(!x.empty())        {            a=x.front();            x.pop();            if(a.time==1)//时间为1,不能往下走了,这是跳出条件            {                if(a.x!=e_x||a.y!=e_y)//如果不是终点,那么必死无疑                    continue;                sum_time[a.x][a.y]=min(sum_time[a.x][a.y],a.sum_time);//否则总时间min一下            }            for(int i=0; i<4; i++)//往四个方向搜索一下            {                b.x=a.x+changex[i];                b.y=a.y+changey[i];                b.sum_time=a.sum_time+1;                if(!in_mat(b.x,b.y))                    continue;                if(mat[b.x][b.y]==1||mat[b.x][b.y]==2||mat[b.x][b.y]==3)                    b.time=a.time-1;                else if(mat[b.x][b.y]==0)                    continue;                else if(mat[b.x][b.y]==4)                    b.time=6;                if(tim[b.x][b.y]<b.time)//如果炸弹时间长于当前值                {                    tim[b.x][b.y]=b.time;                    sum_time[b.x][b.y]=min(sum_time[b.x][b.y],b.sum_time);                    x.push(b);                }            }        }        if(sum_time[e_x][e_y]>100)            puts("-1");        else            printf("%d\n",sum_time[e_x][e_y]);    }    return 0;}
原创粉丝点击