hdu 2612 多终点BFS

本文介绍了一种使用两次BFS算法解决寻找两个角色从不同起点出发,在一张地图上找到距离双方最近的KFC的问题。通过预先计算每个角色到所有KFC的最短路径,再求出所有KFC中两者的总距离最小值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Find a way

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
 

思路:两次BFS存下对每个kfc的最短距离,之后两两相加取min

代码:

 1 #include "cstdio"
 2 #include "stdlib.h"
 3 #include "iostream"
 4 #include "algorithm"
 5 #include "string"
 6 #include "cstring"
 7 #include "queue"
 8 #include "cmath"
 9 #include "vector"
10 #include "map"
11 #include "set"
12 #define mj
13 #define db double
14 #define ll long long
15 using namespace std;
16 const int N=1e8+2;
17 const int mod=1e9+7;
18 //const ll inf=1e16+10;
19 #define inf 0x3f3f3f
20 typedef pair<int,int> P;
21 int n,m;
22 char  s[300][300];
23 int d[300][300],k[205][205];
24 int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
25 int t[2][205*205];
26 int bfs(int sx,int sy,int id)
27 {
28     queue<P> q;
29     for(int i=0;i<205;i++){
30         for(int j=0;j<205;j++){
31             d[i][j]=N;
32         }
33     }
34     q.push(P(sx,sy));
35     d[sx][sy]=0;
36     while(q.size()){
37         P p;
38         p=q.front(),q.pop();
39         for(int i=0;i<4;i++){
40             int nx=p.first+dx[i],ny=p.second+dy[i];
41             if(0<=nx&&nx<n&&0<=ny&&ny<m&&s[nx][ny]!='#'&&d[nx][ny]==N){
42                 d[nx][ny]=d[p.first][p.second]+1;
43                 if(s[nx][ny]=='@') t[id][k[nx][ny]]=d[nx][ny];//到该点的距离
44                 q.push(P(nx,ny));
45             }
46         }
47     }
48     return 0;
49 }
50 int main()
51 {
52     int xx[3],yy[3];
53     while(scanf("%d%d",&n,&m)==2){
54         memset(t,inf, sizeof(t));
55         int c=0,cnt=0,ma=N;
56         for(int i=0;i<n;i++){
57             scanf("%s",s[i]);
58             for(int j=0;j<m;j++){
59                 if(s[i][j]=='Y') xx[c]=i,yy[c++]=j;
60                 else if(s[i][j]=='M') xx[c]=i,yy[c++]=j;
61                 else if(s[i][j]=='@'){
62                     k[i][j]=cnt++;
63                 }
64             }
65         }
66         bfs(xx[0],yy[0],0),bfs(xx[1],yy[1],1);
67         for(int i=0;i<cnt;i++){
68             ma=min(t[0][i]+t[1][i],ma);
69 //            printf("%d %d\n",t[0][i],t[1][i]);
70         }
71         printf("%d\n",11*ma);
72     }
73     return 0;
74 }

 

转载于:https://www.cnblogs.com/mj-liylho/p/7190577.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值