概述
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
|
因为存在走到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;
}
}
最后
以上就是专一百合为你收集整理的Rescue (BFS 优先队列)Rescue的全部内容,希望文章能够帮你解决Rescue (BFS 优先队列)Rescue所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复