#include<iostream>
#include<string>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,di,dj,ok,ss;
char mapp[210][210];
int vis[210][210];
//应该不止一个朋友
struct node{int x,y;int step;friend bool operator < (node a,node b){return a.step > b.step;  //升序
    }
};
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
priority_queue<node> pq;
void  bfs(){node temp,next;int s,d;while(!pq.empty()){temp=pq.top();// cout<<temp.floor<<" "<<temp.x<<" "<<temp.y<<" "<<temp.step<<endl;
        pq.pop();if(temp.x==di&&temp.y==dj){}for(int i=0;i<4;i++){s=temp.x+dir[i][0];d=temp.y+dir[i][1];if(s==di&&d==dj){ok=1;ss=temp.step+1;break;}if(s>=0&&s<n&&d>=0&&d<m&&vis[s][d]==0&&mapp[s][d]!='#'){if(mapp[s][d]=='.'){next.step=temp.step+1;}else if(mapp[s][d]=='x'){next.step=temp.step+2;}next.x=s;next.y=d;pq.push(next);vis[s][d]=1;}}if(ok) break;}}
int main(){int si,sj;while(cin>>n>>m){memset(vis,0,sizeof(vis));while(!pq.empty()) pq.pop();ok=0;ss=0;for(int i=0;i<n;i++)for(int j=0;j<m;j++){cin>>mapp[i][j];if(mapp[i][j]=='a'){di=i;dj=j;}if(mapp[i][j]=='r'){si=i;sj=j;}}node tt;tt.x=si;tt.y=sj;tt.step=0;vis[si][sj]=1;pq.push(tt);bfs();if(ss) cout<<ss<<endl;else  cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;}return 0;
}

转载于:https://www.cnblogs.com/wintersong/p/5231665.html

1242 Rescue BFS相关推荐

  1. HDU 1242 Rescue BFS+优先队列

    题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1242 #include <stdio.h> #include <stri ...

  2. (step4.2.3)hdu 1242(Rescue——BFS)

    题目大意:friends用最短的时间去救angel '.'表示通道 '#'表示墙壁 'x'表示guard.走一格要一单位时间,杀死一个guard要一个单位时间. 如果可以救求最短时间,否则按要求输出 ...

  3. BFS HDOJ 1242 Rescue

    题目传送门 题意:从r走到a,遇到x多走一步,问最小走到a的步数 分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS.我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不 ...

  4. 1242 Rescue

    题目详情: Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  5. HDU 1242 Rescue

    bfs问题. Angel有被关在监狱,她有非常多朋友要去救她. #表示墙,.表示路,x表示警卫,r表示她的朋友. 因为可能有非常多朋友,可是Angel仅仅有一个,所以搜索起点设为Angel.仅仅要找到 ...

  6. 拯救天使 (BFS)

    题目: 1242 Rescue 1 //这是一个比较标准的bfs,没有经过任何优化,但是思路比较清晰,容易看懂. 2 #include <iostream> 3 #include < ...

  7. hdu1242 Rescue DFS(路径探索题)

    hdu1242 Rescue DFS(路径探索题) 这里我定义的路径探索题指 找某路能够到达目的地,每次走都有方向,由于是探索性的走 之后要后退 那些走过的状态都还原掉 地址:http://acm.h ...

  8. 杭电OJ分类题目(1)

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(1) HDU Introduction HDU ...

  9. *【HDU - 1242 】 Rescue (反向dfs,或bfs)

    题干: Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N ...

最新文章

  1. scikit对超参数模型优化对比(网格搜索与随机搜索对比)
  2. Python3猜数字小游戏
  3. Algorithms_入门基础_时间复杂度空间复杂度
  4. 【Python】随心所欲地用Python处理时间
  5. centos linux 内核升级,Centos系统的升级及Linux 内核升级
  6. 玩转SpringSession,重要知识点全面剖析!
  7. python 编程(关于twisted)
  8. 将一个字符串进行反转。将字符串中指定部分进行反转
  9. c#随机产生常用汉字
  10. python标准库模块——json库的用法
  11. 易语言:游戏辅助 CF队伤“卡秒器“ 编程思路/开发者优化建议
  12. 网店营销忌讳效应:要知道别人不喜欢什么
  13. 从12306.cn谈大网站架构与性能优化
  14. 生物信息之独孤九剑——find
  15. Discourse 论坛激活邮件问题
  16. thinksns java_php-java登录thinksns时的加密解密操作
  17. boss直聘python_python分析BOSS直聘的某个招聘岗位数据
  18. typedef与typedef struct
  19. 电子与通信工程与计算机考研哪个好,计算机,通信工程,电子哪个专业研究生出国比较容..._考研_帮考网...
  20. oracle系统架构

热门文章

  1. 湖南网络推广教大家如何“挽回”网站被降权的排名和权重?
  2. 如何提高营销型网站与搜索引擎之间的信任度?
  3. mysql查看死锁并杀掉_查看当前进程,或死锁进程,并能自动杀掉死进程_MySQL
  4. linux内核丢弃udp报文,c++ Linux UDP数据包丢失的原因
  5. java 枚举 被继承_enum不能被继承
  6. 趣谈网络协议笔记-二(第七,八,九讲)
  7. 放弃鼠标,提高效率——像用Vim一样使用FF
  8. 函数式编程——做到并发,不可变数据修改就只能复制后修改返回
  9. tflearn 数据集太大无法加载进内存问题?——使用image_preloader 或者是 hdf5 dataset to deal with that issue...
  10. 信息检索导论的课件——http://home.ustc.edu.cn/~zhufengx/ir/pdf/