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): 514Problem 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.)
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.
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; }}
0 0
- Rescue (BFS 优先队列)
- Rescue(优先队列+bfs)
- rescue(BFS+优先队列)
- hdu 1242Rescue(bfs+优先队列)
- HDU1242 Rescue(优先队列加BFS)
- hdu 1242 Rescue (优先队列+bfs)
- HDU 1242 Rescue(BFS +优先队列)
- 1242Rescue (优先队列BFS)
- HDU 1242 Rescue(BFS,优先队列)
- hdu 1242 Rescue(BFS优先队列)
- HDU 1242 Rescue(优先队列+bfs)
- hdu 1242 Rescue(BFS+优先队列)
- HDU 1242 Rescue(BFS+优先队列)
- hdoj 1242 Rescue (bfs+优先队列)
- hdu 1242 Rescue (BFS+优先队列)
- HDU 1242 Rescue (BFS+优先队列)
- hdu1242 Rescue(优先队列bfs)
- HDU-1242-Rescue(优先队列+BFS)
- Clock
- poj 3468 A Simple Problem with Integers(线段树——区间更新)
- uoj#34多项式乘法
- shell的数值运算方法
- bzoj2179: FFT快速傅立叶
- Rescue (BFS 优先队列)
- bzoj2194: 快速傅立叶之二
- opencv(8)---访问图像像素
- Aizu 2780 Best Matched Pair
- java I/O 之读取InputStream数据到内存&&内存数据写入到OutputSteam中
- bzoj4259: 残缺的字符串
- 使用bootstrap-select 动态加载数据不显示的问题
- bzoj3527: [Zjoi2014]力
- .NET产品源码保护,.NET防止反编译,c#/vb.net 防反编译