HDU-1242 Rescue
来源:互联网 发布:国外的即时通讯软件 编辑:程序博客网 时间:2024/04/27 07:28
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
Sample Output
13
分析:使用全局队列,多点同时bfs求出最短路,若单独求会超时。
Source:
#include<cstdio>#include<iostream>#include<queue>#include<cstring>#include<algorithm>using namespace std;const int maxn=1005;const int INF=0x3f3f3f3f;int N,M;char map[maxn][maxn];bool vis[maxn][maxn];int dx[4]={0,1,0,-1};int dy[4]={1,0,-1,0};int path[maxn][maxn]; //记录步数queue<int> qx;queue<int> qy; //全局队列int bfs(){ while(!qx.empty()&&!qy.empty()) { int x=qx.front(); int y=qy.front(); qx.pop(); qy.pop(); for(int i=0;i<4;i++) { int x1=x+dx[i]; int y1=y+dy[i]; if(x1>=0&&x1<N&&y1>=0&&y1<M&&!vis[x1][y1]&&map[x1][y1]!='#') { qx.push(x1); qy.push(y1); vis[x1][y1]=true; path[x1][y1]=path[x][y]+1; if(map[x1][y1]=='x') path[x1][y1]++; if(map[x1][y1]=='a') return path[x1][y1]; } } } return INF;}int main(){ while(scanf("%d%d",&N,&M)!=EOF) { for(int i=0;i<N;i++) for(int j=0;j<M;j++) cin>>map[i][j]; memset(vis,false,sizeof(vis)); memset(path,-1,sizeof(path)); while(qx.size()) qx.pop(); while(qy.size()) qy.pop(); //初始化,清空队列 for(int i=0;i<N;i++) { for(int j=0;j<M;j++) { if(map[i][j]=='r') { qx.push(i); qy.push(j); //多点入队 vis[i][j]=true; path[i][j]=0; //初始化 } } } int k=bfs(); if(k<INF) cout<<k<<endl; else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; } return 0;}
- hdu 1242 Rescue
- HDU-1242-Rescue
- HDU 1242 Rescue
- HDU-1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue<java>
- hdu 1242 rescue
- HDU-1242:Rescue
- HDU 1242 Rescue
- HDU 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- Rescue hdu 1242
- hdu-1242-Rescue
- Hdu 1242 Rescue
- VUE本地开发第三弹:vue-cli+nginx实现本地开发
- K-diff Pairs in an Array问题及解法
- centos搭建gitlab服务器
- 关于上架App Store 的问题汇总
- 湖南房卡跑胡子棋牌源码,娄底跑胡子\衡阳跑胡子\怀化跑胡子\湘乡跑胡子
- HDU-1242 Rescue
- LD_LIBRARY_PATH .
- 数据库——由ER图转换为关系映射的方法
- ftp服务器搭建
- python简介
- JNI、NDK相关技术学习及使用方法
- 出现 command not found,怎么办???
- Struts2中对Servlet API的访问
- Acegi (Spring Security)入门