博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1242 - Rescue(BFS 优先队列)
阅读量:5084 次
发布时间:2019-06-13

本文共 3338 字,大约阅读时间需要 11 分钟。

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


Solution

题目大意:安琪儿被魔王抓走,她的朋友们想救她,’r’为起点,’a’为终点,’.’为通道,’#’为墙,’x’为守卫,每走一步需要一单位时间,碰到守卫需要多花一单位时间。求救出安琪儿最短需要多少时间。
解题思路:没有守卫的话,这道题就是普通的BFS,加了守卫,所以需要用优先队列,每次取出步数小的点。


Code

#include 
#include
#include
#include
using namespace std;const int INF = 10000000;#define maxn 210struct node{ int x, y, step; bool operator<(const node &b)const { return step>b.step; }};int dir[4][2] = { { -1,0 },{ 0,-1 },{ 1,0 },{ 0,1 } }; //方向int n, m, d[maxn][maxn]; //d为到各个位置最短距离数组char s[maxn][maxn];int bfs(node a){ priority_queue
que; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) d[i][j] = INF; que.push(a); d[a.x][a.y] = 0; while (!que.empty()) { node now = que.top(); que.pop(); int x = now.x, y = now.y, step = now.step; if (s[x][y] == 'a') return step; for (int i = 0; i < 4; i++) { int xx = x + dir[i][0], yy = y + dir[i][1]; if (!(xx<1 || xx>n || yy>m || yy<1 || s[xx][yy] == '#')) { node next; next.x = xx; next.y = yy; next.step = step + 1; if (s[xx][yy] == 'x') next.step++; if (d[xx][yy]>next.step) { d[xx][yy] = next.step; que.push(next); } } } } return INF;}int main(int argc, char const *argv[]){ while (~scanf_s("%d%d", &n, &m)) { node a; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> s[i][j]; if (s[i][j] == 'r') { a.x = i; a.y = j; a.step = 0; } } int ans = bfs(a); if (ans != INF) printf("%d\n", ans); else printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0;}

转载于:https://www.cnblogs.com/aizc/p/7576239.html

你可能感兴趣的文章
Enterprise Library - Data Access Application Block 6.0.1304
查看>>
重构代码 —— 函数即变量(Replace temp with Query)
查看>>
Bootstrap栅格学习
查看>>
程序员的数学
查看>>
聚合与组合
查看>>
jQuery如何获得select选中的值?input单选radio选中的值
查看>>
设计模式 之 享元模式
查看>>
如何理解汉诺塔
查看>>
洛谷 P2089 烤鸡【DFS递归/10重枚举】
查看>>
15 FFT及其框图实现
查看>>
Linux基本操作
查看>>
osg ifc ifccolumn
查看>>
C++ STL partial_sort
查看>>
3.0.35 platform 设备资源和数据
查看>>
centos redis 安装过程,解决办法
查看>>
IOS小技巧整理
查看>>
WebDriverExtensionsByC#
查看>>
我眼中的技术地图
查看>>
lc 145. Binary Tree Postorder Traversal
查看>>
sublime 配置java运行环境
查看>>