HDU2612 Find a way —— BFS

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


 

Find a way

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

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
 

 

Author
yifenfei
 

 

Source

 

 



题解:

开个数组记录两个人到达某个KFC的时间和,然后两人分别进行一次BFS。

坑点: 在bfs完然后更新答案的时候,遇到KFC不一定意味着能更新答案,因为这个KFC可能只有一个人或没有人到达,所以还有开多个数组,记录每个KFC有多少个人能够到达。

 


代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 #define ms(a,b) memset((a),(b),sizeof((a)))
13 using namespace std;
14 typedef long long LL;
15 const int INF = 2e9;
16 const LL LNF = 9e18;
17 const int MOD = 1e9+7;
18 const int MAXN = 200+10;
19 
20 char M[MAXN][MAXN];
21 int vis[MAXN][MAXN], time[MAXN][MAXN], arrived[MAXN][MAXN]; //time为两人到达某个KFC的时间和,arrived为某个KFC的到达人数
22 int n, m, dir[4][2] = {0,1,1,0,0,-1,-1,0};
23 
24 struct node
25 {
26     int x, y, step;
27 };
28 
29 queue<node>que;
30 void bfs(int x, int y)
31 {
32     ms(vis,0);
33     while(!que.empty()) que.pop();
34 
35     node now, tmp;
36     vis[x][y] = 1;
37     now.x = x, now.y = y, now.step = 0;
38     que.push(now);
39 
40     while(!que.empty())
41     {
42         now = que.front();
43         que.pop();
44 
45         for(int i = 0; i<4; i++)
46         {
47             tmp.x = now.x + dir[i][0];
48             tmp.y = now.y + dir[i][1];
49             if(tmp.x>=1 && tmp.x<=n && tmp.y>=1 && tmp.y<=m && !vis[tmp.x][tmp.y] && M[tmp.x][tmp.y]!='#' )
50             {
51                 vis[tmp.x][tmp.y] = 1;
52                 tmp.step = now.step + 1;
53                 que.push(tmp);
54                 if(M[tmp.x][tmp.y]=='@')
55                 {
56                     arrived[tmp.x][tmp.y]++;    //多了一个人到达
57                     time[tmp.x][tmp.y] += tmp.step;       //更新时间之和
58                 }
59             }
60         }
61     }
62 }
63 
64 int main()
65 {
66     while(scanf("%d%d",&n,&m)!=EOF)
67     {
68         for(int i = 1; i<=n; i++)
69             scanf("%s", M[i]+1);
70 
71         ms(time,0);
72         ms(arrived,0);
73         for(int i = 1; i<=n; i++)
74         for(int j = 1; j<=m; j++)
75             if(M[i][j]=='M' || M[i][j]=='Y')
76                 bfs(i,j);
77 
78         int ans = INF;
79         for(int i = 1; i<=n; i++)
80         for(int j = 1; j<=m; j++)
81             if(M[i][j]=='@' && arrived[i][j]==2)    //遇到KFC且两个人都能到达
82                 ans = min(ans, time[i][j]);
83 
84         printf("%d\n",ans*11);
85     }
86 }
View Code

 


 

posted on 2017-09-01 19:55  h_z_cong  阅读(178)  评论(0编辑  收藏  举报

导航