HDU-1072-nightmare

来源:互联网 发布:常用组态软件 编辑:程序博客网 时间:2024/06/04 19:06

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.
 
题意:
额,给你6分钟时间逃出一个迷宫(n*m),给你一个起点,迷宫中有障碍有空地还有炸弹解除器,每走到一个炸弹解除器,剩余时间回复6分钟,解除器可以重复使用,如果到达迷宫出口或者到达炸弹接触器时剩余时间为0,算作失败。

解题思路:
从起点开始BFS搜索,走过的路径可以重复走,但是重复走的到该节点时候要求现在剩余的时间比原来剩余的多(否则没有意义),然后碰到重点就return dist,详见代码:

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<queue>#include<cmath>#include<stdlib.h>#include<cctype>using namespace std;int dx[]={1,-1,0,0};int dy[]={0,0,1,-1};int g[10][10],vis[10][10];int bx,by,ex,ey,m,n;struct Node{        int x,y,rest,dist;        Node(int x1,int y1,int rest1,int dist1):x(x1),y(y1),rest(rest1),dist(dist1){}};queue<Node>q;int bfs(){        memset(vis,0,sizeof vis);        while(!q.empty())        {                q.pop();        }        q.push(Node(bx,by,6,0));        while(!q.empty())        {                Node now=q.front();                q.pop();                //cout<<g[now.x][now.y]<<endl;               // cout<<now.x<<' '<<now.y<<endl;                if(now.rest==0)                        continue;                if(now.x==ex&&now.y==ey)                        return now.dist;                for(int i=0;i<4;i++)                {                        int rest=now.rest-1;                        if(rest==0)                                break;                        int dist=now.dist+1;                        int xx=now.x+dx[i];                        int yy=now.y+dy[i];                        if(g[xx][yy]==4)                                rest=6;                        if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&g[xx][yy]!=0&&now.rest>vis[xx][yy])                        {                                vis[xx][yy]=rest;                                q.push(Node(xx,yy,rest,dist));                        }                }        }        return -1;}int main(){        int T;        cin>>T;        while(T--)        {                cin>>n>>m;                for(int i=1;i<=n;i++)                {                        for(int j=1;j<=m;j++)                        {                                cin>>g[i][j];                                if(g[i][j]==2)                                {                                        bx=i;                                        by=j;                                }                                if(g[i][j]==3)                                {                                        ex=i;                                        ey=j;                                }                        }                        getchar();                }                cout<<bfs()<<endl;        }}





0 0
原创粉丝点击