hdojRescue (BFS)
来源:互联网 发布:华硕笔记本推荐 知乎 编辑:程序博客网 时间:2024/06/06 02:54
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.)
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.
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题意:angel被关在监狱中,她的朋友们前去营救她,给出一个监狱的布局图,a是angel的位置,r是朋友的位置,x是守卫的位置,如果朋友们在营救过程中碰到了守卫,需要花一单位时间去打败守卫,并且每走一步也要花去一单位时间,求成功营救angel所花费的最少时间思路:用bfs和优先队列.代码如下:#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;char map[220][220];int n,m,flag,sx,sy,ex,ey;int dir[4][2]={0,1,1,0,0,-1,-1,0};struct node{int x,y,step; friendbool operator < (node a,node b){return a.step>b.step;}};int judge(node temp){if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m&&map[temp.x][temp.y]!='#') return 1; return 0; }int bfs(){node s;s.x=sx,s.y=sy,s.step=0;priority_queue<node>que;que.push(s);while(!que.empty()){ node now=que.top();que.pop();if(now.x==ex&&now.y==ey){ return now.step;}for(int l=0;l<4;l++){node end;end.x=now.x+dir[l][0];end.y=now.y+dir[l][1]; if(judge(end)) { if(map[end.x][end.y]=='.'||map[end.x][end.y]=='a') end.step=now.step+1; else end.step=now.step+2; map[end.x][end.y]='#'; que.push(end); }}}return -1;}int main(){int i,j;while(scanf("%d%d",&n,&m)!=EOF){if(n==0&&m==0)break;flag=0;for(i=0;i<n;i++)scanf("%s",map[i]);for(i=0;i<n;i++){for(j=0;j<m;j++){if(map[i][j]=='r'){sx=i;sy=j;}if(map[i][j]=='a'){ex=i,ey=j;}}}int ans=bfs();if(ans==-1)printf("Poor ANGEL has to stay in the prison all his life.\n");elseprintf("%d\n",ans);}}
0 0
- hdojRescue (BFS)
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- BFS
- BFS
- BFS
- BFS
- bfs
- BFS
- 安卓首页图片轮播效果(淘宝、京东首页广告效果)
- 数据结构实验之二叉树一:树的同构
- 适合用来做demo或者概念视频的轻音乐 慢慢添加
- 子类extends父类方法执行顺序
- Android 沉浸式状态栏的三种实现方式
- hdojRescue (BFS)
- PHP判断指定时间段的2个方法
- yield and Generators
- timeline播放
- 线段树专题#4_蒟蒻训练历程记录_HDU1698_ 延迟标记、区间更新
- hdu 2509 Be the Winner(博弈Nim)
- Codeforces Round #364 C. They Are Everywhere分析及题解 尺取法
- RxJava之一——一次性学会使用RxJava
- Android使用ViewPager实现左右循环滑动及轮播效果