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 ...
随机推荐
- 记一次 pip list --outdated 错误
在 Windows CMD 执行 pip list --outdated,出现如下错误:" [WinError 10061] 由于目标计算机积极拒绝,无法连接",原因是我之前用的源 ...
- HDU 5889 Barricade(最短路+最小割水题)
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- 【SPOJ1297】Palindrome (SA+RMQ)
求最长回文串.把原串翻转后,加在原串后面,中间插入一个辨别字符.然后求SA,Height.然后枚举每个字母作为回文串中心,分长度为奇数和偶数去讨论:奇数求 suffix(i)和suffix(n-i+1 ...
- 0-Android使用Ashmem机制进行跨进程共享内存
Android使用Ashmem机制进行跨进程共享内存 来源: http://blog.csdn.net/luoshengyang/article/details/6651971 导语: 在Androi ...
- [暑假集训--数论]poj1365 Prime Land
Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- 2721: [Violet 5]樱花
2721: [Violet 5]樱花 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 547 Solved: 322[Submit][Status][D ...
- 存储过程代码生成器Stored Procedure Generator
原文发布时间为:2010-10-26 -- 来源于本人的百度文章 [由搬家工具导入] Stored Procedure Generator (for SQL Server 2000/2005) htt ...
- iOS 判断来电状态CTCallCenter代码块不执行问题的解决
项目中需要检测来电状态,使用了CTCallCenter,在AppDelegate中,代码如下: CTCallCenter *callCenter = [[CTCallCenter alloc] i ...
- 导入Excel表中的数据
第一步:转换导入的文件 private void btnSelectFile_Click(object sender, EventArgs e) { OpenFileDialog ofd = new ...