nyoj 483 nightmare

来源:互联网 发布:淘宝steam离线游戏 编辑:程序博客网 时间:2024/06/05 02:02

描述
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.
输入
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.

输出
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.

样例输入
23 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 3
样例输出
4-1

题目有点长。。意思就是遇到4就是重置炸弹时间,你可以选择重置,也可以选择不重置,只要保证每一步炸弹的剩余时间都部位0就行了。最后输出的最小步数。

又是一道一遍AC的题目,,好开心。。

这个题特别之处就在于标记。vis不再是标记1或0,而是标记到达这个点的时候所剩余的时间。因为这个题所有走过的点均可以再走,如果不分三七二十一全都入队的话时间复杂度就太大了。所以每到达一个点的时候,就看看当前这个点所剩余的时间是不是比上次来的时候剩余的更多了,如果多了,那么就可以入队;如果不多,那就没有再入队的必要了。

#include <stdio.h>#include <queue>#include <string.h>using namespace std;typedef struct node{    int x, y, t, step;//t是剩余时间,step是走的步数}node;int a[10][10], m, n, dirx[4] = {1, -1, 0, 0}, diry[4] = {0, 0, 1, -1}, bx, by, ex, ey, vis[10][10];int bfs(){    queue<node> q;    int i;    node u, v;    u.x = bx;    u.y = by;    u.t = 6;    u.step = 0;    vis[bx][by] = u.t; //标记了当前所剩余的时间。    q.push(u);    while(!q.empty())    {        u = q.front();        q.pop();        if(u.x == ex && u.y == ey && u.t > 0)            return u.step;        for(i = 0 ; i < 4 ; i++)        {            v.x = u.x + dirx[i];            v.y = u.y + diry[i];            v.t = u.t - 1;            v.step = u.step + 1;            if(v.x < 0 || v.y < 0 || v.x >= m || v.y >= n || v.t <= 0 || a[v.x][v.y] == 0 || v.t <= vis[v.x][v.y])//最后这里,如果此时剩余的时间比从前剩余的时间都少,那这个点还何必再入队呢?                continue;            if(a[v.x][v.y] == 4)                v.t = 6;            vis[v.x][v.y] = v.t; //如果剩余的时间还多,那么就把这个点标记为当前的剩余时间。            q.push(v);        }    }    return -1;}int main(){    int i, j, t;    scanf("%d", &t);    while(t--)    {        memset(vis, 0, sizeof(vis));        scanf("%d %d", &m, &n);        for(i = 0 ; i < m ; i++)        {            for(j = 0 ; j < n ; j++)            {                scanf("%d", &a[i][j]);                if(a[i][j] == 2)                {                    bx = i;                    by = j;                }                else if(a[i][j] == 3)                {                    ex = i;                    ey = j;                }            }        }        printf("%d\n", bfs());    }    return 0;}


0 0
原创粉丝点击