概述
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29856 Accepted Submission(s): 10515
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
1 /* 2 Name: HDU--1242--Rescue 3 Copyright: ©2017 日天大帝 4 Author: 日天大帝 5 Date: 22/04/17 19:18 6 Description: BFS,第一次搜索一遍过,留文纪念 7 */ 8 #include<iostream> 9 #include<queue> 10 #include<cstring> 11 using namespace std; 12 struct node{ 13 int x,y,steps; 14 node():steps(0){ 15 }; 16 bool operator<(const node &a)const { 17 return steps>a.steps; 18 } 19 }; 20 int n,m; 21 node s,e; 22 int dir[4][2] = {0,1,0,-1,1,0,-1,0}; 23 char map[205][205]; 24 void bfs(){ 25 priority_queue<node> q; 26 q.push(s); 27 map[s.x][s.y] = '#'; 28 while(!q.empty()){ 29 node a,temp = q.top();q.pop(); 30 for(int i=0; i<4; ++i){ 31 a = temp; 32 a.x += dir[i][0]; 33 a.y += dir[i][1]; 34 if(a.x<0||a.y<0||a.x>=n||a.y>=m|| map[a.x][a.y] == '#')continue; 35 if(a.x == e.x&& a.y == e.y){ 36 cout<<a.steps+1<<endl;return ; 37 } 38 if(map[a.x][a.y] == '.')a.steps++; 39 else a.steps += 2; 40 map[a.x][a.y] = '#'; 41 q.push(a); 42 } 43 } 44 cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; 45 } 46 int main(){ 47 ios::sync_with_stdio(false); 48 49 while(cin>>n>>m){ 50 memset(map,0,sizeof(map)); 51 for(int i=0; i<n; ++i){ 52 cin>>map[i]; 53 for(int j=0; j<m; ++j){ 54 if(map[i][j] == 'r'){ 55 s.x = i;s.y = j; 56 } 57 if(map[i][j] == 'a'){ 58 e.x = i;e.y = j; 59 } 60 } 61 } 62 bfs(); 63 } 64 return 0; 65 }
转载于:https://www.cnblogs.com/evidd/p/6748904.html
最后
以上就是英勇灰狼为你收集整理的HDU--1242--Rescue(BFS)Rescue的全部内容,希望文章能够帮你解决HDU--1242--Rescue(BFS)Rescue所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复