我是靠谱客的博主 完美大山,这篇文章主要介绍ZOJ 1649 Rescue(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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<stdio.h> #include<string.h> #include<string> #include<queue> #include<algorithm> using namespace std; const int maxm=205; const int inf=1<<29; int m,n; int x1,y1,x2,y2; char map[maxm][maxm]; int vis[maxm][maxm]; int w[maxm][maxm]; int dir[4][2]={{1,0},{0,1},{0,-1},{-1,0}}; struct node { int x; int y; int step; int time; bool operator <(const node s)const { return time>s.time; } }; void bfs() { struct node now,pre; priority_queue<node>q; pre.x=x1; pre.y=y1; pre.step=0; pre.time=0; q.push(pre); while(!q.empty()) { pre=q.top(); q.pop(); for(int i=0; i<4; i++) { now.x=pre.x+dir[i][0]; now.y=pre.y+dir[i][1]; now.step=pre.step+1; now.time=pre.time+1; if(now.x>=0&&now.x<m&&now.y>=0&&now.y<n&&map[now.x][now.y]!='#') { if(map[now.x][now.y]=='x') { now.time++; } if(w[now.x][now.y]>now.time) { w[now.x][now.y]=now.time; q.push(now); } } } } return; } int main() { while(scanf("%d%d",&m,&n)!=EOF) { for(int i=0; i<m; i++) { scanf("%s",map[i]); for(int j=0; j<n; j++) { w[i][j]=inf; if(map[i][j]=='a') { x1=i; y1=j; } if(map[i][j]=='r') { x2=i; y2=j; } } } w[x1][y1]=0; bfs(); if(w[x2][y2]==inf) { printf("Poor ANGEL has to stay in the prison all his life.n"); } else { printf("%dn",w[x2][y2]); } } return 0; }

最后

以上就是完美大山最近收集整理的关于ZOJ 1649 Rescue(BFS)的全部内容,更多相关ZOJ内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部