杭电483--Nightmare(Bfs)

Nightmare

时间限制:1000 ms  |  内存限制:65535 KB

难度:4

描述

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.

样例输入
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 
样例输出
-1
上传者

ACM_林志强

这个题题目要求不能标记,但是要注意是4的情况要标记为1;

#include <queue>
#include <cstdio> 
#include <cstring>
#include <iostream>
using namespace std;
int map[10][10], ac[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
struct Maze
{
    int x, y, step, eng;
} r, s, t;
int n, m; 
int Bfs(int a, int b)
{
    r.x = a; r.y = b; r.step = 0; r.eng = 6;
    queue<Maze> Q;
    Q.push(r);
    while(!Q.empty())
    {
        s = Q.front(); Q.pop();
        for(int i = 0; i < 4; i++)
        {
            t.x = s.x + ac[i][0]; 
            t.y = s.y + ac[i][1];
            t.step = s.step + 1;
            t.eng = s.eng - 1;
            if(t.x >= 0 && t.x < n && t.y >= 0 && t.y < m && map[t.x][t.y] != 0 && t.eng > 0)    
            {
                if(map[t.x][t.y] == 3)
                    return t.step;
                if(map[t.x][t.y] == 4)
                {
                    map[t.x][t.y] = 1;    /***********************/
                    t.eng = 6;
                }
                Q.push(t);
            }
        } 
    }
    return -1;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int x, y, i, j;
        scanf("%d %d", &n, &m);
        for(i = 0; i < n; i++)    
            for(j = 0; j < m; j++)
            {
                scanf("%d", &map[i][j]);
                if(map[i][j] == 2)
                {
                    x = i; 
                    y = j;
                }
            }
        printf("%d\n", Bfs(x, y));
    }
    return 0;
}

时间: 2024-10-03 09:20:16

杭电483--Nightmare(Bfs)的相关文章

杭电1242--Rescue(BFS+优先队列)

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20821    Accepted Submission(s): 7429 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is d

杭电ACM1072——Nightmare~~广度优先搜索

题目的意思是:给你一个迷宫,0代表墙,1代表路,2代表起始位置,3代表终点,4代表爆破装置.一开始,你在2的位置,求到3的最少步数. 起初,你只有6秒钟的时间,时间减到0,你没到3的位置,代表不能出去,输出-1.想要增加时间,可以引爆爆破装置,引爆之后,时间重置为6,可以引爆多个.到引爆装置的时候,时间必须大于0,不然没有时间可以引爆. 迷宫最大是8 * 8.用广度优先搜索可以简单的解决问题. 下面的是AC的代码: #include <iostream> #include <cstdio

杭电ACM1242——Rescue~~BFS+优先队列

这题,简单的BFS就可以搞定.题目的大概意思是最短时间从地图的r到达a. 一开始,用普通的队列来做,结果内存超了,原因是N和M最大200:普通的队列会浪费一大堆内存,所以应该改用优先队列来做. 下面是AC的代码: #include <iostream> #include <queue> #include <cstdio> using namespace std; class data { public: int x, y, cost; friend bool opera

杭电2102--A计划(Bfs)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2102 骑士救公主,迷宫问题.做的时候思路不清晰,一直Wa, 其实就是没搞清楚从posa-->posb无论b是传送门还是路都会耗时1, 只不过经过传送门又传送到了下一个位置:一直在这个误区里走不出来:Tmdi.bfs搜一遍就说明搜过来就说明posa一定为点,代码中又把特殊情况全部排除, 所以只有两种情况: '.' --->'#', '.'-->'.'(包含p, 为搜索结束条件), 瞬间明朗了很多啊

Catch That Cow 杭电2717【BFS】

Problem Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. F

杭电 逃离迷宫 BFS

给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去.令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的.我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

杭电 1242 Rescue(广搜)

http://acm.hdu.edu.cn/showproblem.php?pid=1242 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15597    Accepted Submission(s): 5663 Problem Description Angel was caught by the MOLIGPY!