BFS
来源:互联网 发布:Mac air怎么下steam 编辑:程序博客网 时间:2024/05/26 22:10
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1265 Accepted Submission(s): 505Problem 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
#include <iostream>#include <sstream>#include <string.h>#include <cstdio>#include <string>#include <cctype>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <cmath>#define PI acos(-1)#define ll long long#define INF 0x7fffffffusing namespace std;int n, m;char G[205][205];int v[205][205];int sx, sy, tx, ty;const int dx[4] = {0, 1, 0, -1};const int dy[4] = {1, 0, -1, 0};struct P { int x; int y; int step; /// step 小的优先级高,需要先出队。 bool operator < (const P &b) const{ // return abs(x-rx) + abs(y-ry) > abs(b.x-rx) + abs(b.y-ry); return step > b.step; }};int bfs() { memset(v, 0, sizeof v); v[sx][sy] = 1; P cur, next; cur.x = sx; cur.y = sy; cur.step = 0; priority_queue<P> q; q.push(cur); while (q.size()) { P cur = q.top(); q.pop(); if (cur.x == tx && cur.y == ty) { return cur.step; } for (int i = 0; i < 4; i++) { int nx = cur.x + dx[i]; int ny = cur.y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && G[nx][ny] != '#' && v[nx][ny] == 0) { // cout << cur.x << cur.y << "---->>>" << nx << ny << endl; next.x = nx; next.y = ny; v[nx][ny] = 1; if (G[nx][ny] == 'x') { next.step = cur.step + 2; } else { next.step = cur.step + 1; } q.push(next); } } } return -1;}int main(){ while (~scanf("%d%d", &n, &m)) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { //G[i][j] = getchar(); cin >> G[i][j]; if (G[i][j] == 'a') { sx = i; sy = j; } else if (G[i][j] == 'r') { tx = i; ty = j; } } } int step = bfs(); if (step != -1) cout << step << endl; else cout << "Poor ANGEL has to stay in the prison all his life." << endl; } return 0;}
0 0
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- BFS
- BFS
- BFS
- BFS
- bfs
- BFS
- BFS
- Vim指令大全
- 设计模式(十三)抽象工厂模式
- lua 全排列
- 前端学习之路-css选择器和布局
- Leetcode——287. Find the Duplicate Number
- BFS
- 3D游戏引擎系列三
- cadi gc cracked cadi gc 3.06 cadi gc 3.10 cadi gc 3.08
- 使用rtl-usb接收无线电
- GStreamer基础教程08——pipeline的快捷访问
- Centos6.8 防火墙设置
- linux学习 (一 )、win10下安装ubuntu双系统
- clang源码——CompilerInstance和Preprocessor(一)
- 浅析JDBC程序的编写步骤