zoj1649-Rescue (迷宫最短路径)【bfs 优先队列】
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649
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
题意:从'r'起步,目的地为'a','#'不可过,'.'为一步,特殊的是‘x'算两步。求最少步数。
思路:bfs。这里需要注意的是x,因为算两步,在普通的bfs队列里不一定先出列的是最短步,所以得用优先队列。
代码:
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue> using namespace std; #define PI acos(-1.0)
#define EPS 1e-10
#define lll __int64
#define ll long long
#define INF 0x7fffffff struct node{
int x,y,step;
bool operator < (const node &r) const{
return step>r.step;
}
}tmp; priority_queue<node> qu;
int n,m,x1,y1_;
char matrix[][];
bool b[][];
char xy[][]={{,,,-},{,-,,}}; inline bool Check(int x,int y);
int Bfs(); int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
while(~scanf("%d %d",&n,&m)){
for(int i=;i<=m+;i++) matrix[][i]='#',matrix[n+][i]='#';
for(int i=;i<=n+;i++) matrix[i][]='#',matrix[i][m+]='#';
for(int i=;i<=n;i++){
getchar();
for(int j=;j<=m;j++){
matrix[i][j]=getchar();
if(matrix[i][j]=='r') x1=i,y1_=j;
}
}
int ans=Bfs();
if(ans==-) puts("Poor ANGEL has to stay in the prison all his life.");
else printf("%d\n",ans);
}
return ;
}
inline bool Check(int x,int y){
return matrix[x][y]!='#'&&b[x][y]==;
}
int Bfs(){
memset(b,,sizeof(b));
while(!qu.empty()) qu.pop();
tmp.step=;
tmp.x=x1;
tmp.y=y1_;
qu.push(tmp);
b[x1][y1_]=;
while(!qu.empty()){
tmp=qu.top();
qu.pop();
int x=tmp.x,y=tmp.y;
int tx,ty,ts=tmp.step+;
for(int i=;i<;i++){
tx=x+xy[][i];
ty=y+xy[][i];
if(Check(tx,ty)){
if(matrix[tx][ty]=='.'){
b[tx][ty]=;
tmp.x=tx;
tmp.y=ty;
tmp.step=ts;
qu.push(tmp);
}else if(matrix[tx][ty]=='x'){
b[tx][ty]=;
tmp.x=tx;
tmp.y=ty;
tmp.step=ts+;
qu.push(tmp);
}else{
return ts;
}
}
}
}
return -;
}
zoj1649-Rescue (迷宫最短路径)【bfs 优先队列】的更多相关文章
- Rescue HDU1242 (BFS+优先队列) 标签: 搜索 2016-05-04 22:21 69人阅读 评论(0)
Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is describe ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- BFS+优先队列+状态压缩DP+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others) Memo ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
- 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 ...
- hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)
题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- D. Lunar New Year and a Wander bfs+优先队列
D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...
随机推荐
- 纯文本文件 numbers.txt, 里面的内容(包括方括号)如下所示
from collections import OrderedDict import xlwt with open('student.txt','r') as f: number_list = jso ...
- 用过的sql 工具
sequel pro 港优创新 php myadmin 腾讯
- js关于a++ 与++a
var a=6; var b=a++; 此时a的值是7;a++的值是7;b的值是6; var c=8; var d=--c; 此时c的值是7;--c的值是6;d的值是7;
- [转]NSIS:判断D盘存在与否确定安装路径
转载自:http://www.flighty.cn/html/bushu/20140704_239.html 现在我们想实现这样的功能: 如果目标机器存在D盘,那么就安装程序到D盘,否则安装在系统 ...
- jstack可以定位到线程堆栈
java命令--jstack 工具 JVM调优之jstack找出最耗cpu的线程并定位代码 jstack可以定位到线程堆栈,根据堆栈信息我们
- ubuntu 查看系统配置
1, 主板信息 .查看主板的序列号 -------------------------------------------------- #使用 命令 dmidecode | grep -i 'ser ...
- IP地址与子网掩码
IP地址 众所周知,为了确保通信时能相互识别,在internet上的每台主机都必须有一个唯一的标识,即主机的IP地址.IP协议就是根据IP地址来实现信息传递的. IP地址由32位(4字节)二进制数组成 ...
- TACACS+简单说明
1 TACACS+概述 1.1 什么是TACACS+ TACACS+(Terminal Access Controller Access Control System,终端访问控制器控制系统协议)是在 ...
- Mybatis 接口绑定
MyBatis的接口绑定: 参考链接:http://blog.csdn.net/chris_mao/article/details/48836039 接口映射就是在IBatis中任意定义接口,然后把接 ...
- VueCli
Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统,Vue CLI 致力于将 Vue 生态中的工具基础标准化.它确保了各种构建工具能够基于智能的默认配置即可平稳衔接,这样你可以专注在撰写 ...