1242 Rescue【bfs】
来源:互联网 发布:凯迪网络猫眼看人 编辑:程序博客网 时间:2024/06/08 13:24
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21316 Accepted Submission(s): 7598
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.)
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
找最短路径,bfs 题型,因为中间过程可能会影响时间的使用不均衡,所以这个题要用优先队列来解决,优先考虑用时间少的方法,把每一个状态拓展来的状态都放进队列里,注意标记重复的状态.....从天使的位置开始考虑比较好,因为 r 可能有多个,只需要找到最近的那个 r 就行!!
#include<stdio.h>#include<string.h>#include<queue>using namespace std;char x[205][205];int v[205][205],n,m,kase;int fx[4]={-1,0,0,1},fy[4]={0,-1,1,0};struct save{ int x,y,t; friend bool operator<(save a,save b) { return a.t>b.t;//时间少的优先考虑 }}r,t;priority_queue<save> q;void bfs(){ while(!q.empty()) { r=q.top(); q.pop(); if(x[r.x][r.y]=='r')//找到了一个,立刻终止 { kase=1; return; } for(int i=0;i<4;++i)//向四个方向拓展 { int tx=fx[i]+r.x,ty=fy[i]+r.y; t.x=tx; t.y=ty; t.t=r.t+1; if(x[tx][ty]=='#') { continue; } if(x[tx][ty]=='x')//有守卫,多费一个时间 { ++t.t; } if(tx>=0&&tx<n && ty>=0&&ty<m&&!v[tx][ty]) { v[tx][ty]=1;//用过的标记 q.push(t); } } }}int main(){ int i,j,a,b;char y; while(~scanf("%d%d",&n,&m)) { for(i=0;i<n;++i) { getchar(); for(j=0;j<m;++j) { scanf("%c",&y); x[i][j]=y; if(y=='a') { a=i;b=j; } } } while(!q.empty())//注意清空 { q.pop(); } memset(v,0,sizeof(v)); kase=0; r.x=a; r.y=b; r.t=0; q.push(r);v[a][b]=1;//把状态入队列.... bfs(); if(kase==1) { printf("%d\n",r.t); continue; } printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0;}
0 0
- hdoj 1242 Rescue(bfs)
- hdu 1242 Rescue (bfs)
- HDU 1242 Rescue BFS
- HDU--1242:Rescue (BFS)
- hdoj 1242 Rescue (BFS)
- HDU 1242:Rescue 【bfs】
- 1242 Rescue【bfs】
- hdoj 1242 Rescue [BFS]
- HDOJ 1242 Rescue【BFS】
- hdoj 1242 Rescue 【bfs】
- hdu 1242 Rescue(bfs)
- HDU-1242-Rescue【BFS】
- HDU 1242 BFS-Rescue
- HDOJ 1242 Rescue (BFS)
- 【BFS】hdu 1242 Rescue
- hdu 1242 Rescue(bfs)
- HDU-#1242 Rescue(BFS)
- HDU 1242 Rescue (bfs + priority_queue)
- 华为机试题:解析9进制、11进制字符串为10进制数字
- LeetCode #215 Kth Largest Element in an Array
- tomcat jndi配置例子图
- 浅谈NSNotification使用
- Jsp之两个Jsp页面之间传输数据
- 1242 Rescue【bfs】
- network namespace tips
- 第78讲:Type与Class实战详解学习笔记
- Scala模式匹配下提取器构造
- mysql 高可用5
- Putty修改Colorscheme(颜色模式)
- php-IIS和WampServer 冲突问题
- PPT投影仪演示设置
- [学习笔记]JavaScript基础--全选