我是靠谱客的博主 野性星月,这篇文章主要介绍hdu 2612 Find a way 【bfs】,现在分享给大家,希望可以做个参考。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24234    Accepted Submission(s): 7933


 

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#

Sample Output

复制代码
1
2
3
66 88 66

 题意:两个人分别从两个位置出发,求到达终点的时间加和!(终点可能不止一个)

思路:两次bfs+队列的使用,开两个时间数组t1和t2,分别记录两个人各自经过目的点的时间,最后判断一下!

复制代码
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
97
98
99
#include<iostream> #include<algorithm> #include<string.h> #include<queue> using namespace std; #define ms(a) memset(a,0,sizeof a) const int inf = 0x3f3f3f3f; #define ma(b) memset(b,inf,sizeof b) const int maxn = 210; char map1[maxn][maxn]; int vis[maxn][maxn],n,m,x2,y2,x0,y0; int t1[maxn][maxn],t2[maxn][maxn]; struct node{ int x,y,step; }; int dis[4][2]={0,1,0,-1,1,0,-1,0}; bool check(int x,int y) { if(x>=0 && x<n && y>=0 && y<m) return true; return false; } void bfs(int x1,int y1,int q1) { queue<node>q; node e1,e2; e1.x = x1; e1.y = y1; e1.step = 0; vis[e1.x][e1.y] = 1; q.push(e1); while(!q.empty()) { e2 = q.front(); q.pop(); if(map1[e2.x][e2.y] == '@') { if(q1==1) t1[e2.x][e2.y] = e2.step; else t2[e2.x][e2.y] = e2.step; } for(int i=0;i<4;i++) { e1.x = e2.x + dis[i][0]; e1.y = e2.y + dis[i][1]; if(check(e1.x,e1.y) && map1[e1.x][e1.y]!='#' && !vis[e1.x][e1.y]) { e1.step = e2.step + 1; vis[e1.x][e1.y] = 1; q.push(e1); } } } } int main() { while(cin>>n>>m) { for(int i=0;i<n;i++) cin>>map1[i]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(map1[i][j] == 'Y') { x0 = i; y0 = j; } if(map1[i][j] == 'M') { x2 = i; y2 = j; } } } ms(vis); ma(t1); bfs(x0,y0,1); ms(vis); ma(t2); bfs(x2,y2,2); int min1 = inf; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(t1[i][j]!=inf && t2[i][j]!=inf) { min1 = min(t1[i][j]+t2[i][j],min1); } } } cout<<min1*11<<endl; } return 0; }

 

最后

以上就是野性星月最近收集整理的关于hdu 2612 Find a way 【bfs】的全部内容,更多相关hdu内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部