Hdoj 1424 Rescue 【BFS】

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 18493    Accepted Submission(s): 6606

Problem Description

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel‘s friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there‘s a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up,
down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)

Input

First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel‘s friend.

Process to the end of the file.

Output

For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."

Sample Input

7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

Sample Output

13

题意:给你一个图,‘#’代表墙, ‘.’代表路,‘x’代表是士兵。‘a’代表是终点,‘r’是起点,上下左右移动,每移动一步时间加1,遇到士兵要再加1,求需要的最少的

时间。

bfs+priority_queue

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>

const int M = 205;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

using namespace std;

char map[M][M];
bool vis[M][M];
int n, m;
struct node{
    int step;
    int x, y;
    bool operator < (const node & a) const {
        return step > a.step;
    }
};
node st, en;

bool limit(node s){
    return (s.x >=0 && s.x < n&& s.y >= 0&& s.y < m&& map[s.x][s.y] != '#');
}

bool bfs(){
    memset(vis, 0, sizeof(vis));
    vis[st.x][st.y] = 1;
    priority_queue<node >q;
    q.push(st);
    while(!q.empty()){
        node temp = q.top();
        q.pop();
        for(int i = 0; i < 4; ++ i){
            node cur = temp;
            cur.x += dx[i]; cur.y += dy[i];
            if(cur.x == en.x&& cur.y == en.y){
                printf("%d\n", cur.step+1); return 1;
            }
            if(!vis[cur.x][cur.y]&&limit(cur)){
                vis[cur.x][cur.y] = 1;
                if(map[cur.x][cur.y] != '.'){
                    cur.step += 2;
                }
                else cur.step ++;
                q.push(cur);
            }
        }
    }
    return 0;
}

int main(){
    while(scanf("%d%d", &n, &m) == 2){
        for(int i = 0; i < n; ++ i){
            //cin >> map[i];
            scanf("%s", map[i]);
            for(int j = 0; j < m; ++ j)
            if(map[i][j] == 'a'){
                en.x = i; en.y = j;
            }
            else if(map[i][j] == 'r'){
                st.x = i; st.y = j; st.step = 0;
            }
        }
        bool flag = bfs();
        if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
} 
时间: 2024-09-29 09:00:01

Hdoj 1424 Rescue 【BFS】的相关文章

HDU1242 Rescue 【BFS】

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

HDOJ 1495 非常可乐 【BFS】

非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5954    Accepted Submission(s): 2428 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要

hdoj 1072 Nightmare 【bfs】

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8568    Accepted Submission(s): 4107 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ti

hdoj 1312 Red and Black 【BFS】

题意:一共有四个方向,从'@'出发,找能到达'.'的个数, #是不能通过的. 策略:广搜. 这道题属于最简单的bfs了. 代码: #include<stdio.h> #include<string.h> #include<queue> using std::queue; bool vis[25][25]; char s[25][25]; int n, m; int ans = 0; struct node{ int x, y; }; node st; const int

【bfs】【中等难度】tyvj P1234 - bench与奔驰

P1234 - bench与奔驰 From zhangbh001    Normal (OI) 总时限:10s    内存限制:128MB    代码长度 限制:64KB P1234 - bench与奔驰 背景 Background 公园里有个人在练开奔驰 - -!,但是总是撞在bench上 (众人曰:狼来了,快跑啊!) 描述 Description 公园里的bench与奔驰都是无敌的,不会被撞坏.由于开奔驰的人比较"有特点",总是向上下左右四个方向开,而且只会在撞到椅子之后改变方向(

HDU 1253 胜利大逃亡 NYOJ 523【BFS】

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24608    Accepted Submission(s): 9427 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

NYOJ 284 坦克大战 【BFS】+【优先队列】

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that co

【BFS】uva10047The Monocycle

/* 本题的特殊之处,到达一个格子时,因为朝向不同,以及接触地面的颜色不同, 会处于不同的状态::::::::: 把(x, y, d, c)作为一个结点,表示所在位置(x, y),方向为d,颜色为c;;;;; ------------------------------------------------------------------------ 在方向上我们把前,左,右编号为0,1,2:::: 颜色,从蓝色开始编号为0,1,2,3:::::::::: ------------------

【BFS】uva11624Fire!

/* bfs宽度遍历 -------------------------------------------------------------------------- 对人和火同时进行bfs,,注意应该先火后人,即如果在人到达该格子前,格子已经着火 则不应该走,最后人走到边界无路可走,则IMPOSSIBLE!!!!!!!!!!!! --------------------------------------------------------------------------- 两次bfs