hdu1242 Rescue bfs+优先队列
直接把Angle的位置作为起点,广度优先搜索即可,这题不是步数最少,而是time最少,就把以time作为衡量标准,加入优先队列,队首就是当前time最少的。遇到Angle的朋友就退出。只需15ms
AC代码:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=202;
int d[maxn][maxn];
int n,m;
char G[maxn][maxn];
struct node{
int x,y;
node(){
}
node(int x,int y):x(x),y(y){
}
bool operator <(const node &p)const{
return d[x][y]>d[p.x][p.y];
}
}s;
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
int bfs(){
memset(d,-1,sizeof(d));
priority_queue<node>q;
d[s.x][s.y]=0;
q.push(s);
while(!q.empty()){
node p=q.top();
q.pop();
int x=p.x,y=p.y;
if(G[x][y]=='r') return d[x][y];
for(int i=0;i<4;++i){
int px=x+dx[i],py=y+dy[i];
if(px<0||py<0||px>=n||py>=m||d[px][py]!=-1||G[px][py]=='#') continue;
if(G[px][py]=='x') d[px][py]=d[x][y]+2;
else d[px][py]=d[x][y]+1;
q.push(node(px,py));
}
}
return -1;
}
void print(){
for(int i=0;i<n;++i){
for(int j=0;j<m;++j)
printf("%02d ",d[i][j]);
printf("\n");
}
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=0;i<n;++i)
scanf("%s",G[i]);
for(int i=0;i<n;++i)
for(int j=0;j<m;++j){
if(G[i][j]=='a') s=node(i,j);
else if(G[i][j]!='r'&&G[i][j]!='a'&&G[i][j]!='.'&&G[i][j]!='#') G[i][j]='x';
}
int ans=bfs();
//print();
if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.\n");
else printf("%d\n",ans);
}
}
如有不当之处欢迎指出!
hdu1242 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 +优先队列 or BFS )
http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意: Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...
- 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 ...
- poj1649 Rescue(BFS+优先队列)
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- 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.....#..#.##...##...#.... ...
随机推荐
- junit4X系列源码--总体介绍
原文出处:http://www.cnblogs.com/caoyuanzhanlang/p/3530267.html.感谢作者的无私分享. Junit是一个可编写重复测试的简单框架,是基于Xunit架 ...
- SQL模板资源管理器,你用了吗?
SQL Server Management Studio 有个模板资源管理器,不知你用过没有?使用模板创建脚本.自定义模板等功能能大大提高你的工作效率,如果没有尝试过,赶紧去试试吧.很多时候,我们习惯 ...
- MySQL中, 如何查询某一天, 某一月, 某一年的数据.
今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天(包括昨天和今天的数据) SELECT * FROM 表名 WHERE TO_ ...
- TensorFlow实现线性回归模型代码
模型构建 1.示例代码linear_regression_model.py #!/usr/bin/python # -*- coding: utf-8 -* import tensorflow as ...
- linux下安装pip
1.pip下载安装 wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d ...
- 使用facebook和twitter进行分享经验总结
凤凰涅槃,浴火重生. 在传说当中,凤凰是人世间幸福的使者,每五百年,它就要背负着积累于在人间的所有痛苦和恩怨情仇,投身于熊熊烈火中自焚,以生命和美丽的终结换取人世的祥和与幸福.同样在肉体经受了巨大的痛 ...
- softmax_cross_entropy_with_logits
softmax_cross_entropy_with_logits 原创文章,请勿转载 函数定义 def softmax_cross_entropy_with_logits(_sentinel=Non ...
- 一道python面试题引发的血案
这里说的是一道阿里校招的面试题:一行代码实现对列表a中的偶数位置的元素进行加3后求和? 今天去面试同样遇到了这个题目,这道题考察的是对python高阶函数map/filter的灵活运用(具体的使用方法 ...
- javascript 中 dom.getAttribute("value") 与dom.value的差异
dom 是一个 input type="text" 手动修改 input 的值, 使用 dom.getAttribute("value") 只能得到 html ...
- http目录显示时间与服务器相差8小时
一直用nginx做http服务,代码里访问过文件地址,并未认真关注过访问http目录下的时间戳.今天浏览文件的时候发现一个问题.web上显示的文件时间戳与服务器时间相比差8个小时.具体表现看下图: w ...