Rescue (BFS 优先队列)

来源:互联网 发布:php是最好的语言 编辑:程序博客网 时间:2024/05/18 03:00

Rescue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1279 Accepted Submission(s): 514
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
 


题意:给出一个n行m列的图,'#'代表墙,'.'代表守卫,'.'代表路,'a'代表angel,'r'代表angel的朋友。angel的朋友要接近angel,可以走'.'和'x',每走一步需要1个单位的时间,当经过x所在的位置时,要杀死x才能继续往前走,但杀死x同样要消耗1个单位的时间。问从r到a最少需要多长时间;如果不能到达,输出”Poor ANGEL has to stay in the prison all his life.”。


因为存在走到x加2, 走到.加1的问题,队列的优先级会发生变化,为了得到最优解,我们考虑使用优先队列priority_queue

#include <iostream>#include <algorithm>#include <string.h>#include <cmath>#include <stdio.h>#include <vector>#include <map>#include <queue>#include <utility>typedef long long ll;using namespace std;char m[205][205];int visited[205][205];int M, N;int mov[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};struct node {    int x, y, step;    bool operator < (const node& n) const{        return n.step < step;    }};bool judge(int x, int y) {    if (x < 0 || x >= N || y < 0 || y >= M || m[x][y] == '#' || visited[x][y])        return 0;    return 1;}int bfs(int start_x, int start_y) {    priority_queue<node> que;    node first;    first.x = start_x, first.y = start_y, first.step = 0;    que.push(first);    visited[first.x][first.y] = 1;    while (!que.empty()) {        node cur = que.top();        que.pop();        for (int i = 0; i < 4; ++i) {            int x = cur.x + mov[i][0];            int y = cur.y + mov[i][1];            int time = cur.step;            if (judge(x, y)) {                node new_node;                new_node.x = x;                new_node.y = y;                if (m[x][y] == 'a')                    return time + 1;                if (m[x][y] == '.')                    time++;                else if (m[x][y] == 'x')                    time += 2;                visited[x][y] = 1;                new_node.step = time;                que.push(new_node);            }        }    }    return -1;}int main () {    int start_x, start_y;    while (cin >> N >> M) {        memset(visited, 0, sizeof(visited));        memset(m, 0, sizeof(m));        for (int i = 0; i < N; ++i)            for (int j = 0; j < M; ++j) {                cin >> m[i][j];                if (m[i][j] == 'r')                    start_x = i, start_y = j;            }                int ans = bfs(start_x, start_y);        if (ans != -1)            cout << ans << endl;        else            cout << "Poor ANGEL has to stay in the prison all his life." << endl;    }}



0 0
原创粉丝点击