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

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

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 20938    Accepted Submission(s): 7486

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
题意:问最小花时间从起点到终点,其中要到守卫的地方要多花1个时间。
思路:由于其中有若干步要花费2个时间,可以用优先队列存储,走花费时间少的。用DFS枚举的会超时。
收获:优先队列定义结构体优先级。
1 #include
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 #define maxn 200+10 8 int n,m,flag; 9 struct Node10 {11 int x,y,cost;12 bool operator < (const Node &a)const//定义时间花费少的优先级高。13 {14 return cost>a.cost;15 }16 };17 Node st,et,f,k;18 int dx[]={
0,0,-1,1};19 int dy[]={
1,-1,0,0};20 int vis[maxn][maxn];21 char map[maxn][maxn];22 void bfs()23 {24 priority_queue
q;25 q.push(st);26 while(!q.empty())27 {28 f=q.top();29 for(int i=0;i<4;i++)30 {31 k.x=f.x+dx[i];32 k.y=f.y+dy[i];33 if(!vis[k.x][k.y]&&k.x>=0&&k.x
=0&&k.y

 

转载于:https://www.cnblogs.com/ZP-Better/p/4698762.html

你可能感兴趣的文章
浏览器性能测试网址
查看>>
[MTK FP]用Python把图片资源image.rar中为.pbm后缀的文件更改为.bmp后缀的方法
查看>>
实验二
查看>>
[LeetCode]203. Remove Linked List Elements 解题小结
查看>>
测试一下
查看>>
vue base64
查看>>
【Django实战开发】案例一:创建自己的blog站点-1.安装及搭建开发环境
查看>>
Pie(二分)
查看>>
Mysql 索引优化
查看>>
09湖州二模(自选模块不等式)
查看>>
Mybatis Batch 批量操作
查看>>
Ubuntu server搭建Java web服务器
查看>>
WSGI学习系列WSME
查看>>
java读取xml配置文件和properties配置文件
查看>>
HDU 4300 Contest 1
查看>>
POJ 3311
查看>>
Button MouseEvent颜色变化
查看>>
Volist标签
查看>>
浅谈模块化
查看>>
14个免费访客行为分析工具
查看>>