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. Android基础TOP4:Tost的使用

    Activity: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xm ...

  2. Mac OS 小知识

         删除Mac OS输入法中自动记忆的用户词组 有时候不小心制造了一个错误的词组,结果也被输入法牢牢记住,这时候可以用shift+delete组合键来删除      快捷键拾遗 Fn+Delet ...

  3. javascript模块化编程(一)(http://www.ruanyifeng.com/blog/2012/10/javascript_module.html)

    Javascript模块化编程(一):模块的写法   随着网站逐渐变成"互联网应用程序",嵌入网页的Javascript代码越来越庞大,越来越复杂. 网页越来越像桌面程序,需要一个 ...

  4. -moz、-ms、-webkit

    1.-moz代表firefox浏览器私有属性 2.-ms代表IE浏览器私有属性 3.-webkit代表safari.chrome私有属性 需要设置这个的样式: transform,border-rad ...

  5. B.1 接口

    几乎所有要学习的接口都位于 System.Collections.Generic 命名空间.图B-1展示了.NET4.5以前主要接口间的关系,此外还将非泛型的 IEnumerable 作为根接口包括了 ...

  6. JavaScript:一句代码输出重复字符串(字符串乘法)

    看到一个题目要求写一个函数times,输出str重复num次的字符串. 比如str:bac     num:3 输出:abcabcabc 除了利用循环还有几种方法,我学习研究之后记下以下三种方法. 1 ...

  7. elasticsearch 数据导入(九)

    说明 maven依赖 官方客户端 https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.4/index.html <d ...

  8. 对SHH的公钥和私钥的简单理解

    SSH是在应用层和传输层基础上的安全协议 SSH提供了两种级别的安全验证: 第一基于密码的安全验证:账号.密码,但可能有别的服务器冒充真正的服务器,无法避免被“中间人”攻击(man-in-the-mi ...

  9. E - Period

    For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...

  10. insert into varchar2(8000)

    在看12c的文档的时候发现varcahr2最大长度是4000 byte VARCHAR2 Data Type The VARCHAR2 data type specifies a variable-l ...