杭电1072——Nightmare(BFS)

来源:互联网 发布:电子表格删除重复数据 编辑:程序博客网 时间:2024/06/01 07:54

Problem 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
1 1 3
4 8
2 1 1 0 1 1 1 0
1 0 4 1 1 0 4 1
1 0 0 0 0 0 0 1
1 1 1 4 1 1 1 3
5 8
1 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
-1
13

题目大意:

迷宫中有一定时炸弹,6分钟后将会爆炸,有人困在迷宫中,每个时刻只能往上下左右四个方向相邻的单元格中走,不能走到墙上去,也不能走到迷宫外,如果走到炸弹重置的格子中,且剩余时间大于0,则剩余时间被重置为6。每个格子可以走多次。问人能否在炸弹爆炸前走出迷宫,且到达迷宫时的剩余时间大于0。如果能,则输出走出迷宫所需的最短时间,否则,输出-1。

主要算法:

该题运用了BFS,首先从起点开始对迷宫进行一次宽度优先遍历,看能否到达终点,且到达时剩余时间大于0。如果能,则直接输出到达终点的最短时间,如果不能,则从所有重置单元格开始宽度优先遍历,看能否达到终点,且剩余时间大于0,如果存在,则在所有能到达终点的路径中找一条时间最短的路径,输出最短时间。否则,输出-1.

AC代码:

# include<iostream># include<queue>using namespace std;struct Point{    int i,j,len,time,val;    bool operator < (const Point &p) const{        return len>p.len;//距离最小值优先    }};int t,m,n;struct Point maze[9][9],start,end1,reset[70];int visited[9][9],resetNum,flag=0;int d[4][2]={{-1,0},{1,0},{0,-1},{0,1}};int bfs(struct Point start);void input(int x[],int y[]);int main(){    int i,x[70],y[70];    cin>>t;    while(t--)    {        resetNum=0;        input(x,y);        start.len=0;        start.time=6;        int len=bfs(start);        if(len==-1)        {            for(i=0;i<resetNum;i++)            {                int tlen=bfs(maze[x[i]][y[i]]);                if(len==-1||tlen<len)                    len=tlen;            }        }        printf("%d\n",len);    }    return 0;}void input(int x[],int y[]){    int i,j;    cin>>n>>m;    for(i=0;i<n;i++)    {        for(j=0;j<m;j++)        {            cin>>maze[i][j].val;            maze[i][j].i=i;            maze[i][j].j=j;            if(maze[i][j].val==2)                start=maze[i][j];            else if(maze[i][j].val==3)                end1=maze[i][j];            else if(maze[i][j].val==4)            {                x[resetNum]=i;                y[resetNum++]=j;            }        }    }}//宽度优先搜索,能够到达终点则返回到达终点的距离,失败返回-1int bfs(struct Point start){    memset(visited,0,sizeof(visited));    priority_queue <Point> queue;    queue.push(start);    visited[start.i][start.j]=1;    int i;    while(!queue.empty())    {        Point t=queue.top();        if(t.i==end1.i&&t.j==end1.j)        {            if(t.time<=0)//到达终点时时间为0,会爆炸                return -1;            else                return t.len;        }        queue.pop();        if(t.time<=0)            continue;        for(i=0;i<4;i++)        {            int nx=t.i+d[i][0];            int ny=t.j+d[i][1];            if(nx>=0&&nx<n&&ny>=0&&ny<m&&!visited[nx][ny]&&maze[nx][ny].val!=0)            {                maze[nx][ny].len=t.len+1;                maze[nx][ny].time=t.time-1;                if(maze[nx][ny].val==4&&maze[nx][ny].time>0)//重置时间时,时间为0,爆炸                    maze[nx][ny].time=6;                queue.push(maze[nx][ny]);                visited[nx][ny]=1;            }        }    }    return -1;}
0 0
原创粉丝点击