hdu 1242(BFS+优先队列)
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29263 Accepted Submission(s): 10342
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.)
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.
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."
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
int vis[N][N];
int n,m;
char mp[N][N];
int _x[]={,,-,};
int _y[]={,,,-};
struct node{
int x,y;
int time;
friend bool operator < (node a,node b){
return a.time>b.time;
}
}a,b;
priority_queue<node>q;
int judge(int x,int y){
if(x<||x>=n||y<||y>=m)return ;
if(vis[x][y]==)return ;
if(mp[x][y]=='#')return ;
return ;
}
int BFS(int x,int y){
a.time=;
a.x=x;a.y=y;
q.push(a);
vis[x][y]=;
while(!q.empty()){
b=q.top();
q.pop();
for(int i=;i<;i++){
int xx=b.x+_x[i];
int yy=b.y+_y[i];
if(judge(xx,yy)){
vis[xx][yy]=;
if(mp[xx][yy]=='x')a.time=b.time+;
else if(mp[xx][yy]=='r')return (b.time+);
else
a.time=b.time+;
a.x=xx;
a.y=yy;
q.push(a);
}
}
}
return -;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
while(!q.empty())q.pop();
memset(vis,,sizeof(vis));
int x,y;
for(int i=;i<n;i++)
for(int j=;j<m;j++){
cin>>mp[i][j];
if(mp[i][j]=='a'){
x=i;y=j;
//a.x=x;a.y=y;a.time=0;
}
}
int ans=BFS(x,y);
if(ans==-)
printf("Poor ANGEL has to stay in the prison all his life.\n" );
else
printf("%d\n",ans);
}
}
hdu 1242(BFS+优先队列)的更多相关文章
- HDU 1242 (BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- hdu 1242 Rescue_bfs+优先队列
翻出以前的代码看看 题意:走迷宫,遇到敌人要花一分钟. #include<iostream> #include<queue> using namespace std; char ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
- hdu Rescue (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 简单优先队列搜索,自己好久不敲,,,,,手残啊,,,,orz 代码: #include < ...
随机推荐
- Unity引擎 UGUI
Unity UGUI讲解 1.导入UI图片资源 2.设置参数: TextureType(纹理类型) 精灵 2D and UI SpriteMode(精灵模式) Single(单) multiple( ...
- html5——DOM扩展
元素获取 1.document.getElementsByClassName ('class') 通过类名获取元素,以类数组形式存在. 2.document.querySelector(‘div’) ...
- VC++文件监控 ReadDirectoryChangesW
#include <windows.h> #include <tchar.h> #include <stdio.h> #include <assert.h&g ...
- 【sqli-labs】 less57 GET -Challenge -Union -14 queries allowed -Variation4 (GET型 挑战 联合查询 只允许14次查询 变化4)
双引号闭合 http://192.168.136.128/sqli-labs-master/Less-57/?id=1"%23 和less56一样查数据
- seam的定时轮巡
青岛的项目要做一个功能,每天凌晨2点的时候保存一次设备数据,这个就要求项目能够间隔24小时每天去做这个事,是一个自主轮巡. seam框架正好提供了这个功能,@Expiration指定开始时间,@Int ...
- Memcached 之PHP实现服务器集群一致性hash算法
/** * memcached 一致性hash,分布式算法 * Class MemcacheCluster */ class MemcacheCluster { protected $nodes = ...
- linu学习第一天:基础知识
1 bc 计算器 2 ibase=2 以二进制输入,输出10进制 3 obase=2 输出二进制 4 enable --查看内部命令 5 #第一天的命令 6 enable --查看内部命令 7 ena ...
- Linxu基础入门
Linux命令大全:http://man.linuxde.net/ 创建目录 使用 mkdir 命令创建目录 mkdir $HOME/testFolder 切换目录 使用 cd 命令切换目录 cd $ ...
- 第2章 Python序列
Python序列类似于C或Basic中的一维.多维数组等,但功能要强大很多,使用也更加灵活.方便,Head First Python一书就戏称列表是“打了激素”的数组. Python中常用的序列结构有 ...
- Scrapy——6 APP抓包—scrapy框架下载图片
Scrapy——6 怎样进行APP抓包 scrapy框架抓取APP豆果美食数据 怎样用scrapy框架下载图片 怎样用scrapy框架去下载斗鱼APP的图片? Scrapy创建下载图片常见那些问题 怎 ...