我是靠谱客的博主 朴实雪糕,这篇文章主要介绍hdu2612Find a way bfs,现在分享给大家,希望可以做个参考。

http://acm.hdu.edu.cn/showproblem.php?pid=2612

复制代码
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
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki. Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes. Input The input contains multiple test cases. Each test case include, first two integers n, m. (2<=n,m<=200). Next n lines, each line included m character. ‘Y’ express yifenfei initial position. ‘M’ express Merceki initial position. ‘#’ forbid road; ‘.’ Road. ‘@’ KCF Output For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet. Sample Input 4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...# Sample Output 66 88 66

求Y,M到同一@的最短路径
用两次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
#include<iostream> #include<cstdio> #include<string.h> #include<algorithm> #include<queue> #include<stdlib.h> #define inf 0x6ffffff using namespace std; #define N 205 int vis[N][N],m,n,b[N][N],dir[4][2]={ {1,0},{-1,0},{0,1},{0,-1} }; char maps[N][N]; struct node { int x,y,step; }; void bfs(node s) { queue<node> Q; node q; s.step=0; Q.push(s); vis[s.x][s.y]=1; while(Q.size()) { q=Q.front(); Q.pop(); if(maps[q.x][q.y]=='@') { b[q.x][q.y]+=q.step; } for(int i=0;i<4;i++) { s.x=q.x+dir[i][0];//相当于Y,M边走边找 s.y=q.y+dir[i][1]; if(s.x>=0&&s.x<n&&s.y>=0&&s.y<m&&vis[s.x][s.y]==0&&maps[s.x][s.y]!='#') { vis[s.x][s.y]=1; s.step=q.step+1; Q.push(s); } } } } int main() { int i,j; node s1,s2; while(scanf("%d%d",&n,&m)!=EOF) { memset(maps,0,sizeof(maps)); memset(b,0,sizeof(b)); memset(vis,0,sizeof(vis)); for(i=0;i<n;i++) { for(j=0;j<m;j++) { cin>>maps[i][j]; if(maps[i][j]=='Y') { s1.x=i;s1.y=j; } if(maps[i][j]=='M') { s2.x=i;s2.y=j; } } } bfs(s1); memset(vis,0,sizeof(vis)); bfs(s2); int Min=inf; for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(b[i][j]!=0)//不能写成if(maps[i][j]=='@',可能两个人都到不了) { Min=min(Min,b[i][j]); } } } printf("%dn",Min*11); } return 0; }

最后

以上就是朴实雪糕最近收集整理的关于hdu2612Find a way bfs的全部内容,更多相关hdu2612Find内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部