我是靠谱客的博主 热心大雁,这篇文章主要介绍hdu1242(BFS+优先队列),现在分享给大家,希望可以做个参考。

BFS+优先队列的题目,很好写,就发上来水一篇。。。

复制代码
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
74
75
76
77
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; int n,m; const int maxn = 500; char s[maxn][maxn]; int d[maxn][maxn]; int dx[4]={0,1,0,-1}; int dy[4]={1,0,-1,0}; typedef pair<int,int> pr; struct node{ int x,y; int time; node(int x,int y,int time):x(x),y(y),time(time){} bool operator < (const node &a) const { return time>a.time; } }; const int oo = 0x3f3f3f3f; int bfs(int sx,int sy) { for(int i=0; i<maxn; i++) for(int j=0; j<maxn; j++) d[i][j] = oo; priority_queue<node>q; d[sx][sy] = -1; q.push(node(sx,sy,0)); while(!q.empty()) { node tmp = q.top(); q.pop(); if(s[tmp.x][tmp.y]=='r') return tmp.time; for(int i=0; i<4; i++) { int tx = tmp.x+dx[i]; int ty = tmp.y+dy[i]; if(tx>=0&&tx<n&&ty>=0&&ty<m&&s[tx][ty]!='#'&&d[tx][ty]==oo) { if(s[tx][ty]=='x') q.push(node(tx,ty,tmp.time+2)); else q.push(node(tx,ty,tmp.time+1)); d[tx][ty] = -1; } } } return -1; } int main() { while(~scanf("%d%d",&n,&m)) { for(int i=0; i<n; i++) { scanf("%s",s[i]); } int ans = -1; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) if(s[i][j]=='a') { ans = bfs(i,j); break; } } if(ans == -1) printf("Poor ANGEL has to stay in the prison all his life.n"); else printf("%dn",ans); } return 0; }

最后

以上就是热心大雁最近收集整理的关于hdu1242(BFS+优先队列)的全部内容,更多相关hdu1242(BFS+优先队列)内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部