poj1649 Rescue(BFS+优先队列)
Rescue
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
广度优先搜索找最短时间。
看代码凝视吧
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
struct node
{
int x,y,time;//x,y方格的位置。time当前全部的时间
friend bool operator<(node a,node b)//优先队列依照时间大小排序
{
return a.time>b.time;
}
};
priority_queue<node>s;
char map[205][205];//地图
int m,n,vis[205][205],escape,dir[4][2]={0,1,0,-1,1,0,-1,0};//vis标记,dir表示四个方向
bool judge(int x,int y)//推断当前位置时候能够走
{
if(x>=0&&y>=0&&x<n&&y<m&&!vis[x][y]&&map[x][y]!='#')
return true;
else
return false;
}
int tonum(int x,int y)//把对应的道路,警卫换算成时间
{
if(map[x][y]=='x')
return 2;
else
return 1;
}
void bfs(int x,int y)//广度优先搜索
{
node temp,temp1;
temp.time=0,temp.x=x,temp.y=y;
vis[x][y]=1;
s.push(temp);//把temp入队列
while(!s.empty())
{
temp=s.top(),s.pop();
temp1=temp;
if(map[temp.x][temp.y]=='a')//提前结束循环,由于用的优先队列。所以当前找到的肯定是最小的
{
escape=temp.time;
break;
}
for(int i=0;i<4;i++)
{
int xx=temp.x+dir[i][0];
int yy=temp.y+dir[i][1];
if(judge(xx,yy))
{
vis[xx][yy]=1;//当前位置已浏览 标记为1
temp.x=xx,temp.y=yy,temp.time=temp.time+tonum(xx,yy);
s.push(temp);//更新队列
}
temp=temp1;//由于才推断了一个方向。所以还须要temp1保持上次的位置
}
}
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
escape=0;
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
for(int i=0;i<n;i++)
scanf("%s",map[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(map[i][j]=='r')
{
bfs(i,j);
break;
}
if(escape)
printf("%d\n",escape);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
while(!s.empty())//一定要记得清队列。 。刚刚wa了一次
s.pop();
}
}
poj1649 Rescue(BFS+优先队列)的更多相关文章
- HDU1242 Rescue(BFS+优先队列)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu1242 Rescue bfs+优先队列
直接把Angle的位置作为起点,广度优先搜索即可,这题不是步数最少,而是time最少,就把以time作为衡量标准,加入优先队列,队首就是当前time最少的.遇到Angle的朋友就退出.只需15ms A ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- hdu1242 Rescue(BFS +优先队列 or BFS )
http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意: Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...
- Rescue BFS+优先队列 杭电1242
思路 : 优先队列 每次都取最小的时间,遇到了终点直接就输出 #include<iostream> #include<queue> #include<cstring> ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
随机推荐
- Log4j官方文档翻译(五、日志输出的方法)
日志类提供了很多方法用于处理日志活动,它不允许我们自己实例化一个logger,但是提供给我们两种静态方法获得logger对象: public static Logger getRootLogger() ...
- BZOJ-1043 [HAOI2008]下落的圆盘
几何题... 先把所有圆储存起来,然后对于每个圆我们求得之后放下的圆挡住了的部分,求个并集,并把没被挡到的周长加进答案. #include <cstdlib> #include <c ...
- nodejs + express 入门之 hello world
使用nodejs 开发web项目时原生的api开发比较困难.组合express后就比较容易了. 1.安装 express: (express地址: http://www.expressjs ...
- 【CF1073D】Berland Fair(模拟)
题意:初始有t元,每次从1开始买,从1到n依次有n个人,每个人的东西价格为a[i],该人依次能买就买,到n之后再回到1从头开始,问最后能买到的东西数量 n<=2e5,t<=1e18,a[i ...
- PE文件格式---节和节表
17.1.4 节表和节 从排列位置来看,PE文件在DOS部分和PE文件头部分以后就是节表和多个不同的节(如图17.1中的③和④所示).要理解什么是节表,什么是节以及它们之间的关系,那就首先要了解Wi ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---12
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- Day 22 Object_oriented_programming 3
isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象,如果是返回True 1 class F ...
- C#Json转Xml格式数据的方法
第一种方法 string Xml = "在这里写Json字符串"; XmlDictionaryReader reader = JsonReaderWriterFactory.Cre ...
- LeetCode OJ-- Count and Say
https://oj.leetcode.com/problems/count-and-say/ 求经过n次变换后,变成了什么. 1 11 21 1211 111221 ps. 3 变成 ‘3 ...
- Android学习--ListView
这篇文章用于总结自己这两天学到的安卓的ListView和RecyclerView 的笔记,以及从我这个iOS开发者的角度去理解和学习这两个控件,会比较一下他们个iOS中那些控件是一致的,可以用来对比的 ...