博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
G - Rescue 【地图型BFS+优先队列(有障碍物)】
阅读量:5892 次
发布时间:2019-06-19

本文共 2760 字,大约阅读时间需要 9 分钟。

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.) 

InputFirst 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. 
OutputFor 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
/*救人,你在a,要救的人在r,'.'是路可以走,'#'是墙,'x'是警卫,走路花费一秒,遇见警卫花费两秒。问最小多少秒能救人,能的话输出步数,不能的话输出"Poor ANGEL has to stay in the prison all his life."*/#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long#define inf 0x3fffffffusing namespace std;const int maxn = 205;int n,m,xxx,yyy;char a[maxn][maxn];int vis[maxn][maxn];int dir[][2]={ { 1,0},{ 0,1},{-1,0},{ 0,-1} };struct Node{ int xx,yy,time; //横纵坐标+时间 friend bool operator < (Node a,Node b) { return a.time > b.time; }};int check(int x,int y){ if(x<0||x>=n||y<0||y>=m||a[x][y]=='#'||vis[x][y]) return 0; return 1;}/*int check(int x,int y){ if(x<0 || y<0 || x>=n || y>=m || !vis[x][y] || a[x][y] == '#') return 1; return 0;}*/int bfs(int x,int y){ memset(vis,0,sizeof(vis));//清空标记数组放到bfs里面!!!!! priority_queue
q; while(!q.empty()) q.pop(); q.push(Node{x,y,0}); vis[x][y]=1; while(!q.empty()) { Node u=q.top(); q.pop(); Node tmp; if(a[u.xx][u.yy]=='r') return u.time; for(int i=0;i<4;i++) { tmp.xx=u.xx+dir[i][0]; tmp.yy=u.yy+dir[i][1]; if( check(tmp.xx,tmp.yy) ) { vis[tmp.xx][tmp.yy]=1; if(a[tmp.xx][tmp.yy]=='x') tmp.time=u.time+2; else tmp.time=u.time+1; q.push(tmp); } } } return -1;}int main(){ memset(vis,0,sizeof(vis)); while(~scanf("%d%d",&n,&m)){ for(int i=0;i
View Code

 

转载于:https://www.cnblogs.com/Roni-i/p/7407102.html

你可能感兴趣的文章
Spring AOP项目应用——方法入参校验 & 日志横切
查看>>
TestNG 六 测试结果
查看>>
用Fiddler或Charles进行mock数据搭建测试环境
查看>>
使用REST-Assured对API接口进行自动化测试
查看>>
GitHub发布史上最大更新,年度报告出炉!
查看>>
王潮歌跨界指导HUAWEI P20系列发布会 颠覆传统 眼界大开!
查看>>
王高飞:微博已收购一直播 明年一季度重点是功能与流量打通
查看>>
趣头条发行区间7至9美元 预计9月14日美国上市
查看>>
新北市长侯友宜:两岸交流应从隔壁最亲近的人开始
查看>>
全面屏的Nokia X即将上线,不到2000元的信仰你要充值吗?
查看>>
HTML5音频audio属性
查看>>
ES6学习
查看>>
Centos7搭建Django环境
查看>>
序列化一个Intent
查看>>
JavaScript数据类型及语言基础--ife
查看>>
进阶 Nginx 高手必须跨越的 5 座大山
查看>>
国内首例:飞步无人卡车携手中国邮政、德邦投入日常运营
查看>>
“迁移策略+新容器运行时”应对有状态应用的冷热迁移挑战
查看>>
2019数据库趋势报告,最受欢迎的是MySQL
查看>>
敏捷的忠实拥护者David Hussman于8月18日去世
查看>>