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 ...
随机推荐
- FZU 2041 二分枚举
思路:先O(n)预处理出ri[i][j],le[i][j],分别表示第i个位置向右边移动出j个空格需要的步数,表示第i个位置向左边移动出j个空格需要的步数. 然后枚举间隙处,二分判段最大间隔. #in ...
- 【bzoj3585】mex 线段树 mex,sg
Description 有一个长度为n的数组{a1,a2,…,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. Input 第一行n,m. 第二行为n个数. 从第三行开始,每行一个询问l, ...
- Java数据结构-------Map
常用Map:Hashtable.HashMap.LinkedHashMap.TreeMap 类继承关系: HashMap 1)无序: 2)访问速度快: 3)key不允许重复(只允许存在一个null ...
- 【HDOJ5517】Triple(二维BIT)
题意:给你n个二元组<a,b>, m个三元组<c,d,e>. 如果d = e,那么<a,c,d>会组成一个新的三元组集合G. 问G中有多少个三元组在凸点.(没有其它 ...
- 页面get post等查看
原文发布时间为:2010-03-08 -- 来源于本人的百度文章 [由搬家工具导入] http://www.fiddler2.com/Fiddler2/firstrun.asp
- compiler related
1. 词法分析 词法分析器根据词法规则识别出源程序中的各个记号(token),每个记号代表一类单词(lexeme).源程序中常见的记号可以归为几大类:关键字.标识符.字面量和特殊符号.词法分析器的输入 ...
- [LeetCode] Populating Next Right Pointers in Each Node 深度搜索
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- linux 下 多进程与多线程
[Linux]多进程与多线程之间的区别 http://blog.csdn.net/byrsongqq/article/details/6339240 网络编程中设计并发服务器,使用多进程与多线程 ,请 ...
- MMU介绍【转】
转自:http://blog.csdn.net/martree/article/details/3321578 虚拟存储器的基本思想是程序,数据,堆栈的总的大小可以超过物理存储器的大小,操作系统把当前 ...
- bytearray和file的后端上传方式
public static String readAndUpload(String serverpath,String imgid) { if(serverpath==null){ serverpat ...