我是靠谱客的博主 爱笑高跟鞋,这篇文章主要介绍HDU1242或ZOJ1649(BFS+优先队列),现在分享给大家,希望可以做个参考。

题意:
天使被困在监狱,他的朋友们想找到他,监狱的地形复杂,.表示路,#表示,a表示天使的位置,r表示他的朋友,监狱里还有守卫(x表示守卫),他的朋友只能向左右上下四个方向走,每走一步花费一秒时间,若碰上守卫,消灭守卫需要额外花费一秒时间。求最少时间天使能见到他的朋友。
坑点就是有一些人在ZOJ能过,在HDU不能过,是因为数据不同,说起来HDU的数据也很水,明明就一个天使,从天使跑就行了,结果WA了,然后我从每个朋友那里走,求出到天使最小的时间就过了。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<stdio.h> #include<queue> #include<string.h> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f const int MAXN=200+7; struct node { int x,y,t; bool operator<(const node &a)const{ return t>a.t; } }last,now; char s[MAXN][MAXN]; bool vis[MAXN][MAXN]; int g[4][2]={1,0,-1,0,0,1,0,-1}; int n,m,ans; void bfs(int x1,int y1) { memset(vis,false,sizeof(vis)); priority_queue<node>q; last.x=x1; last.y=y1; last.t=0; vis[x1][y1]=true; q.push(last); while(!q.empty()) { last=q.top(); q.pop(); for(int i=0;i<4;i++) { now.x=last.x+g[i][0]; now.y=last.y+g[i][1]; now.t=last.t+1; if(now.x<0||now.x>=n||now.y<0||now.y>=m||s[now.x][now.y]=='#'||vis[now.x][now.y]) continue; if(s[now.x][now.y]=='x') now.t++; if(s[now.x][now.y]=='a') { ans=min(ans,now.t); return ; } vis[now.x][now.y]=true; q.push(now); } } return ; } int main() { while(~scanf("%d%d",&n,&m)) { int x1,y1; ans=INF; for(int i=0;i<n;i++) scanf("%s",s[i]); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) if(s[i][j]=='r'){ bfs(i,j);//因为是多个朋友去找天使,所以要多次BFS找到最小的时间 } } if(ans>=INF) printf("Poor ANGEL has to stay in the prison all his life.n"); else printf("%dn",ans); } }

最后

以上就是爱笑高跟鞋最近收集整理的关于HDU1242或ZOJ1649(BFS+优先队列)的全部内容,更多相关HDU1242或ZOJ1649(BFS+优先队列)内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部