H - Rescue hdu1242
来源:互联网 发布:javascript !! 编辑:程序博客网 时间:2024/06/10 12:04
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
#include<stdio.h>#include<queue>#include<cstring>using namespace std;int visit[220][220];char map[220][220];int a1,b1,c1,d1,n,m;struct node{int x,y,time; friend bool operator<(node n1,node n2){ return n2.time<n1.time;}};int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};int bound(int a,int b){ if(a<0||b<0||a>=n||b>=m||map[a][b]=='#'||!visit[a][b]) return 1; return 0;}int bfs(){priority_queue<node>q;node next,t;t.x=a1;t.y=b1;t.time=0;q.push(t);visit[a1][b1]=0;while(!q.empty()){ t=q.top(); q.pop(); if(t.x==c1&&t.y==d1) return t.time; for(int i=0;i<4;i++) { next=t; next.x=t.x+dir[i][0]; next.y=t.y+dir[i][1]; if(bound(next.x,next.y)) continue; next.time++; if(map[next.x][next.y]=='x') next.time++; if(visit[next.x][next.y]>=next.time) { visit[next.x][next.y]=next.time; q.push(next); } }}return 0;}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { for(int i=0;i<n;i++) {scanf("%s",map[i]); for(int j=0;map[i][j];j++) { if(map[i][j]=='r') { a1=i; b1=j; } else if(map[i][j]=='a') { c1=i;d1=j; } } } memset(visit,1,sizeof(visit)); int z=0; z=bfs(); if(z) printf("%d\n",z); else printf("Poor ANGEL has to stay in the prison all his life.\n"); }}
0 0
- H - Rescue hdu1242
- hdu1242 rescue
- hdu1242 Rescue
- hdu1242 Rescue
- HDU1242 Rescue
- hdu1242 Rescue
- HDU1242 Rescue
- HDU1242-Rescue
- HDU1242-Rescue
- HDU1242 Rescue
- (HDU1242)Rescue
- Rescue (hdu1242)
- HDU1242 Rescue 【BFS】
- hdu1242——Rescue
- hdu1242 rescue【BFS】
- hdu1242(Rescue)
- hdu1242 Rescue(BFS)
- HDU1242 Rescue(BFS松弛)
- 刨根问底-Redis数据结构剖析
- Linux每日学习(十三)
- ios 在状态栏上做动画
- iOS-根据文本的长度确定label的宽高
- SVN提交时'!svn/me'
- H - Rescue hdu1242
- OC Block、Swift Closure以及C# Lambda的对比
- 仿微信联系人索引列表ListView
- "互联网+"系列(9) 腾讯:用"互联网+"连接未来 下篇
- Linux每日学习(十四)
- postgresql_fdw 跨数据库查询
- Unity学习笔记(4) --- Unity的界面排版: RectTransform
- Java IO流之如何选择流对象总结
- 第一章自学spring简介,spring核心系列博客的介绍