NYOJ 483 Nightmare

3人阅读 评论(0) 收藏 举报
分类:

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

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

题目大意:

输入一个数a,表示接下来有a组测试数据。输入x,y表示接下来有一个x*y的矩阵。其中2表示起点;3表示终点;1表示道路;0表示墙,不可通过;4表示炸弹重置时间的点。Ignatius从2出发,身上有一个定时炸弹,时间为6。到4时,只要炸弹时间不为1,即可重置为6。到终点3时,时间也不可为1。问Ignatius走出迷宫的最少步数,如果走不出,输出-1。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int m[10][10];
int s[4][2]={1, 0, -1, 0, 0, 1, 0, -1};
int x,y,x1;
struct zd
{
    int a,b,c,d;
};
int bfs(int a,int b)
{
    int c,d,e,f,g;
    zd i,j,w;
    i.a=a;i.b=b;i.c=0;i.d=6;
    queue<zd>t;
    t.push(i);
    while(!t.empty())
    {
        j=t.front();
        t.pop();
        if(j.d==1)
            continue;
        for(c=0;c<4;c++)
        {
            d=j.a+s[c][0];
            f=j.b+s[c][1];
            if(m[d][f]==3&&d>=0&&d<x&&f>=0&&f<y)
                return j.c+1;
            x1++;
            if(m[d][f]==4&&d>=0&&d<x&&f>=0&&f<y)
            {
                m[d][f]=1;
                i.a=d;i.b=f;i.c=j.c+1;i.d=6;
                t.push(i);
            }
            else if(m[d][f]==1&&d>=0&&d<x&&f>=0&&f<y)
            {
                i.a=d;i.b=f;i.c=j.c+1;i.d=j.d-1;
                t.push(i);
            }
        }
    }
    return -1;
}
int main()
{
    int a,b,c,d,e,f,i,j;
    cin>>a;
    while(a--)
    {
        cin>>x>>y;
        for(d=0;d<x;d++)
        {
            for(e=0;e<y;e++)
            {
                cin>>m[d][e];
                if(m[d][e]==2)
                {
                    i=d;j=e;
                }
            }
        }
        x1=0;
        f=bfs(i,j);
        cout<<f<<endl;
    }
}       



查看评论

nyoj 483 Nightmare 【BFS + priority_queue】

 Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Ignatius had a nightmare last night. H...
  • chenzhenyu123456
  • chenzhenyu123456
  • 2015-07-01 17:28:14
  • 297

NYOJ 483 Nightmare(bfs)

Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Ignatius had a nightmare last night. He found him...
  • helloiamclh
  • helloiamclh
  • 2015-07-27 17:36:54
  • 212

NYOJ 483 Nightmare 【广搜】+【无标记】

Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Ignatius had a nightmare last night. ...
  • u012846486
  • u012846486
  • 2014-06-15 21:04:53
  • 911

nyoj 483 Nightmare

Nightmare时间限制:1000 ms  |  内存限制:65535 KB难度:4描述Ignatius had a nightmare last night. He found himself i...
  • zhangliu463884153
  • zhangliu463884153
  • 2018-04-21 14:26:42
  • 0

nyoj 483 nightmare

描述 Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. Th...
  • Roosevelty
  • Roosevelty
  • 2015-02-03 19:48:54
  • 228

nyoj 483 Nightmare 【bfs】

Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Ignatius had a nightmare last night. He fo...
  • dxx_111
  • dxx_111
  • 2015-09-08 15:36:21
  • 190

NYOJ 483 Nightmare

Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Ignatius had a nightmare last night. He found ...
  • zlj17839192925
  • zlj17839192925
  • 2016-04-22 14:15:40
  • 139

NYOJ 483 Nightmare (无标记BFS)

题目483 题目信息 运行结果 本题排行 讨论区 Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Ignatius had a n...
  • a2459956664
  • a2459956664
  • 2016-06-11 07:16:54
  • 147

HDU 10722 && nyoj 483 Nightmare【bfs】

Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Ignatius had a nightmare last night. He found ...
  • liuke19950717
  • liuke19950717
  • 2015-09-02 16:36:31
  • 275

题目483:Nightmare

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=483描述Ignatius had a nightmare last night. He f...
  • qq_25245961
  • qq_25245961
  • 2017-10-10 11:03:42
  • 50
    个人资料
    持之以恒
    等级:
    访问量: 2万+
    积分: 1730
    排名: 2万+