我是靠谱客的博主 谨慎大侠,最近开发中收集的这篇文章主要介绍hdu 1242 Rescue(bfs+优先队列),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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
 
bfs+优先队列...
bfs能找到最优解的原因是...时间更短的点一定是先访问到先入队...
但是由于守卫的存在...先访问到的节点如果是守卫的话就不是时间更短的节点...
所以这个时候我们要用优先队列...
第一次写.
然后学习了重载<的写法...写在结构体里面和外面的..
感觉q老师...窝只是请教他这个重载在写结构体里面要怎样...他就把kuangbin的模板发了我一份
学到了不少.
还有就是,因为朋友有多个,而小天使只有一个...
不如让小天使去找朋友...逆向思维...
一遍ac,开心.

1 /*************************************************************************

2 
> File Name: code/hdu/1242.cpp

3 
> Author: 111qqz

4 
> Email: rkz2013@126.com

5 
> Created Time: 2015年10月02日 星期五 14时38分17秒

6  ************************************************************************/

7

8 #include<iostream>

9 #include<iomanip>
 10 #include<cstdio>
 11 #include<algorithm>
 12 #include<cmath>
 13 #include<cstring>
 14 #include<string>
 15 #include<map>
 16 #include<set>
 17 #include<queue>
 18 #include<vector>
 19 #include<stack>
 20 #include<cctype>
 21 using namespace std;
 22 #define yn hez111qqz
 23 #define j1 cute111qqz
 24 #define ms(a,x) memset(a,x,sizeof(a))
 25 #define lr dying111qqz
 26 const int dx4[4]={1,0,0,-1};
 27 const int dy4[4]={0,-1,1,0};
 28 typedef long long LL;
 29 typedef double DB;
 30 const int inf = 0x3f3f3f3f;
 31 const int N=2E2+5;
 32 char maze[N][N];
 33 int m,n;
 34 int ans;
 35 bool vis[N][N];
 36 struct node
 37 {
 38
int x,y;
 39
int d;
 40
 41
bool ok ()
 42 
{
 43
if (x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&maze[x][y]!='#') return true;
 44
return false;
 45 
}
 46
bool hasguard()
 47 
{
 48
if (maze[x][y]=='x') return true;
 49
return false;
 50 
}
 51
bool goal()
 52 
{
 53
if (maze[x][y]=='r') return true;
 54
return false;
 55 
}
 56 }s;
 57
 58 bool operator<(const node &a,const node &b)
//重载优先级的<关系...返回true时优先级更小,在队列的更后面.
 59 {
 60
return a.d>b.d;
 61 }
 62
 63 bool bfs()
 64 {
 65
priority_queue<node>q;
 66 
q.push(s);
 67
 68
while (!q.empty())
 69 
{
 70
node pre = q.top();
 71 
q.pop();
 72 //
cout<<pre.x<<" "<<pre.y<<" "<<pre.d<<endl;
 73
if (pre.goal())
 74 
{
 75
ans = pre.d;
 76
return true;
 77 
}
 78
 79
for ( int i = 0 ; i < 4 ; i++)
 80 
{
 81 
node next;
 82
next.x = pre.x + dx4[i];
 83
next.y = pre.y + dy4[i];
 84
next.d = pre.d + 1;
 85
if (!next.ok()) continue;
 86
vis[next.x][next.y] = true;
 87
if (next.hasguard()) next.d++;
 88 
q.push(next);
 89
 90
 91 
}
 92 
}
 93
return false;
 94 }
 95 int main()
 96 {
 97 
#ifndef
ONLINE_JUDGE
 98
freopen("in.txt","r",stdin);
 99
#endif
100
while(scanf("%d %d",&n,&m)!=EOF){
101
ms(vis,false);
102
for ( int i = 0 ; i < n ; i++) scanf("%s",maze[i]);
103
for ( int i = 0 ; i < n ; i++)
104 
{
105
for ( int j = 0 ; j < m ; j++)
106 
{
107
if (maze[i][j]=='a')
108 
{
109
s.x = i ;
110
s.y = j ;
111
s.d = 0 ;
112
vis[i][j] = true;
113
break;
114 
}
115 
}
116 
}
117
118
if (bfs())
119 
{
120
printf("%dn",ans);
121 
}
122
else
123 
{
124
puts("Poor ANGEL has to stay in the prison all his life.");
125 
}
126 }
127  #ifndef ONLINE_JUDGE
128 
fclose(stdin);
129
#endif
130
return 0;
131 }
View Code

 

 
 

转载于:https://www.cnblogs.com/111qqz/p/4853964.html

最后

以上就是谨慎大侠为你收集整理的hdu 1242 Rescue(bfs+优先队列)的全部内容,希望文章能够帮你解决hdu 1242 Rescue(bfs+优先队列)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部