HDOJ 1242 Rescue(bfs+优先队列)
2015-08-06 09:43
429 查看
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21127 Accepted Submission(s): 7549
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.)
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
题意:Angel被抓起来关在了监狱里,给出监狱地图,#表示围墙,.表示道路,a是Angel的位置,r是她朋友的位置,x是监狱守卫的位置(碰到监狱守卫是必须杀死他,花费时间一秒)。在监狱中只能上下左右前进,每前进一格花费一秒。问是否能从r到达a,若能,输出最小时间。
题解:将点的坐标和从r到此处的步数存入一个结构体中,按照步数少的优先排序。 每次遍历优先队列中的首元素,即可找到到达r的最少步数。
代码如下:
<span style="font-size:12px;">#include<cstdio> #include<queue> using namespace std; int n,m,x,y,ex,ey,ans; int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; char map[205][205]; struct node { int x,y,step; }a,temp; bool operator < (const node &x,const node &y) { return x.step>y.step; } int ok(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() { int i; a.x=x; a.y=y; a.step=0; priority_queue<node>q; q.push(a); while(!q.empty()) { a=q.top(); q.pop(); if(a.x==ex&&a.y==ey) return a.step; for(i=0;i<4;i++) { temp.x=a.x+dir[i][0]; temp.y=a.y+dir[i][1]; if(ok(temp)) { if(map[temp.x][temp.y]=='.'||map[temp.x][temp.y]=='a') temp.step=a.step+1; else temp.step=a.step+2; map[temp.x][temp.y]='#'; q.push(temp); } } } return -1; } int main() { int i,j; while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<n;i++) { scanf("%s",map[i]); for(j=0;j<m;j++) { if(map[i][j]=='r') { x=i; y=j; } if(map[i][j]=='a') { ex=i; ey=j; } } } 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; }</span>
相关文章推荐
- HDU 1242:Rescue 【bfs】
- 多个UIImage合并成一个UIImage
- UICollectionView初探
- 【iOS学习笔记】生成带文本的UIImage
- Message MessageQueue Runnable Looper 和Handler的关系理解
- UI0805_Page
- UI0805_CellView
- UI06_UIPageControl
- UI06_UIScrollview
- UI06_ScrollviewLoop
- 使用UIScrollView 结合 UIImageView 实现图片循环滚动
- UI0804_Palette
- UI05_Control
- UI05_手势识别器
- UISwitch
- UI04_TargetAction
- UI04_Delegate
- UIPageControl
- UI04_Touch
- UI03_UIViewController视图控制器