hdu 1242 Rescue
来源:互联网 发布:cocostudio 1.6 mac 编辑:程序博客网 时间:2024/06/03 09:25
问题描述
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.)
输入
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.
输出
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."
样例输入
7 8#.#####.#.a#..r.#..#x.....#..#.##...##...#..............
样例输出
13
这题与poj坦克大战那道题类似http://blog.csdn.net/qq_31237061/article/details/50593233
遇到卫兵多花1秒时间;
思路:bfs+优先队列:
代码:
#include <iostream>#include <queue>#include <cstdio>#include <cstring>using namespace std;struct node{ int x,y,step; node(int a,int b,int c) { x=a;y=b;step=c; } bool operator < (const node & a)const { return step> a.step; }};const int maxx=205;priority_queue <node> que;char ma[maxx][maxx];int vis[maxx][maxx];int n,m;int xx,yy,ex,ey;int dx[]={1,0,-1,0};int dy[]={0,1,0,-1};int bfs(){ node no(xx,yy,0); vis[xx][yy]=1; que.push(no); while(!que.empty()) { node no1=que.top(); que.pop(); for(int i=0;i<4;i++) { int x=no1.x+dx[i]; int y=no1.y+dy[i]; int step=no1.step; if(vis[x][y]||x<0||x>=n||y<0||y>=m||ma[x][y]=='#')continue; if(ma[x][y]=='x') step++; if(ma[x][y]=='a') return step+1; vis[x][y]=1; que.push(node(x,y,step+1)); } } return -1;}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { while(!que.empty())que.pop(); memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { scanf("%s",&ma[i]); for(int j=0;j<m;j++) { if(ma[i][j]=='r') { xx=i;yy=j; } } } int c; c=bfs(); if(c!=-1) printf("%d\n",c); else printf("Poor ANGEL has to stay in the prison all his life.\n"); }}
0 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
- App 混淆
- ZOJ1046
- iOS集成微信支付问题, 友盟SDK有干扰或者说是冲突的解决
- Swift_字符串
- C# 导出数据到Excel模板中
- hdu 1242 Rescue
- MariaDB Galera Cluster 部署
- C#导出EXCEL的几种方法
- 【Android 开发】深入理解内存缓存类LruCache:源码分析
- iOS设置textView的行间距
- Cloudera之旅 ☞ 【二】Spark-sql部署
- 类成员函数作为线程函数
- IOS中对于多个按钮,选中其中一个,其他按钮选中状态为NO
- MySQL性能优化的最佳20+条经验