Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29263    Accepted Submission(s): 10342

Problem Description
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
Author
CHEN, Xue
Source
题意:一个公主被抓  公主有很多个朋友  在图上  a代表公主  r 代表公主的朋友 #代表墙  .代表路   x代表守卫
打败守卫花费2秒  其它一秒   现在问你那个朋友能最快解救公主
因为朋友不止一个  我们可以BFS从公主开始搜 由于打败守卫花的时间长  我们选择优先队列  每次去最小 
这题目数据很水  错的代码都能过  dfs应该也能过
 #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+优先队列)的更多相关文章

  1. HDU 1242 (BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...

  2. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  3. hdu 1242 Rescue_bfs+优先队列

    翻出以前的代码看看 题意:走迷宫,遇到敌人要花一分钟. #include<iostream> #include<queue> using namespace std; char ...

  4. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  5. hdu 1242 找到朋友最短的时间 (BFS+优先队列)

    找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...

  6. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  7. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

  8. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  9. hdu Rescue (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 简单优先队列搜索,自己好久不敲,,,,,手残啊,,,,orz 代码: #include < ...

随机推荐

  1. 微信自定义分享功能实现Tips

    以MVC为例 前台js通过.post()方法传给后台特定Controller当前页面的url,后台获取后,进行处理: 1.获取access_token:https://mp.weixin.qq.com ...

  2. Android开发高手课笔记 - 01 崩溃优化(上):关于“崩溃”那点事

    Android 的两种崩溃 Java 崩溃就是在 Java 代码中,出现了未捕获的异常,导致程序异常退出 Native 崩溃一般都是因为在 Native 代码中访问非法地址,也可能是地址对齐出了问题, ...

  3. vs for Mac中的启用Entity Framework Core .NET命令行工具

    在vs for Mac的工具菜单中已没有了Package Manager Console. 我们可以通过以下方法使用Entity Framework Core .NET命令行工具: 1.添加Nuget ...

  4. c++将bool变量以文字形式打印

    #include <iostream> // std::cout, std::boolalpha, std::noboolalpha int main () { bool b = true ...

  5. profiler-gpu分析记录

    查看 Android 手机芯片信息下面以 夜神模拟器为例 D:\cmderλ adb devices # 1. 列出安卓设备List of devices attached127.0.0.1:6200 ...

  6. css 样式 解释

    字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...

  7. LINQ简记(2):重要概念

    为了能让初学者更快速地掌握,在系列文章中,我尽可能地避开理论讲解,一则对于入门者来说,过多的理论叙述反而会降低大家学习编程的兴趣,二则,官方文档的资料很详细,我说了也是废话.因此,我会尽可能地多举些简 ...

  8. [LeetCode] 887. Super Egg Drop 超级鸡蛋掉落

    You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is iden ...

  9. 个人学习记录--取表中Name相同的最大值,非Group By,可延伸

    ), qy ), je INT); INSERT INTO @t SELECT '产品一', '北京', UNION ALL SELECT '产品一', '上海', UNION ALL SELECT ...

  10. ganlgia-rrdcached

    一.介绍 rrdcached是一个高性能的RRD缓存守护进程,在不带来大量磁盘读/写文件i/o负荷的情况下,允许gmetad实例维护多个RRD文件.rrdcached可通过命令套接字控制,并且包含在大 ...