ZOJ第一题_1649Rescue(优先队列下的BFS)
来源:互联网 发布:windows r2是什么意思 编辑:程序博客网 时间:2024/06/01 17:04
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
一遍AC。。。
#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <queue>using namespace std;const int maxn=205;char map[maxn][maxn];bool vis[maxn][maxn];struct Node{ int x; int y; int step;};struct cmp{ bool operator()(Node &X,Node &Y) { return X.step>Y.step; }};priority_queue<Node,vector<Node>,cmp>q;int dx[]={-1,0,1,0};int dy[]={0,-1,0,1};int N,M;int sx,sy,ex,ey;bool OK(Node cur){ if(cur.x<0||cur.x>=N||cur.y<0||cur.y>=M) return false; return true;}void ReadIn(){ int i,j; for(i=0;i<N;i++) { for(j=0;j<M;j++) { cin>>map[i][j]; if(map[i][j]=='r') { sx=i; sy=j; } if(map[i][j]=='a') { ex=i; ey=j; } } }}int BFS(){ int i; Node first,next,cur; while(!q.empty()) q.pop(); memset(vis,0,sizeof(vis)); first.x=sx; first.y=sy; first.step=0; vis[first.x][first.y]=true; q.push(first); while(!q.empty()) { cur=q.top(); q.pop(); if(cur.x==ex&&cur.y==ey) return cur.step; for(int i=0;i<4;i++) { next.x=cur.x+dx[i]; next.y=cur.y+dy[i]; if(OK(next)&&!vis[next.x][next.y]&&map[next.x][next.y]!='#') { vis[next.x][next.y]=true; if(map[next.x][next.y]=='.') { next.step=cur.step+1; q.push(next); } else if(map[next.x][next.y]=='a') { next.step=cur.step+1; q.push(next); } else if(map[next.x][next.y]=='x') { next.step=cur.step+2; q.push(next); } } } } return -1;}int main(){ int i; while(scanf("%d%d",&N,&M)!=EOF) { ReadIn(); int ans=BFS(); if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",ans); } return 0;}
- ZOJ第一题_1649Rescue(优先队列下的BFS)
- ZOJ 1649 - Rescue BFS/优先队列
- zoj 3103 Cliff Climbing 优先队列+BFS
- zoj 1649 Rescue【BFS+优先队列】
- HDU 1242 && ZOJ 1649( BFS (队列 || 优先队列)).
- zoj 2849 优先队列bfs 熊猫烧香病毒传播
- Hdu 1242 & Zoj 1649 Rescue (优先队列+BFS)
- ZOJ 1649 && HDU 1242 Rescue (BFS + 优先队列)
- ZOJ Problem Set - 1649 Rescue BFS+优先队列
- Zoj 1671 Walking Ant(BFS+优先队列||记忆化搜索)
- ZOJ 2849 Attack of Panda Virus(BFS+优先队列)
- POJ 3635 基于优先队列的BFS
- hdu1180诡异的楼梯(BFS+优先队列)
- hdu 1242 优先队列的bfs
- hdu1180-诡异的楼梯(bfs+优先队列)
- GDUT Krito的讨伐(bfs&&优先队列)
- Krito的讨伐(bfs加上优先队列)
- hdu5253-连接的管道(优先队列+bfs)
- 黑马程序员-JAVA面向对象之抽象、接口、多态
- Socket.close()和OutputStreamWriter.close()有什么区别么?
- 变量和函数的存储类别
- stl map的基本使用
- 解决实体类噩梦:联合实体类(Java反射+泛型实际应用)
- ZOJ第一题_1649Rescue(优先队列下的BFS)
- WMAppManifest.xml修改,图文超详细
- input点击链接另一个页面,各种操作
- 线性表(一)
- 特征向量及特征值的几何意义
- 啦啦啦
- Xshell传输文件
- OCP-1Z0-053-V12.02-346题
- 腾讯2014校园招聘笔试试题