hdu 2612 多终点BFS

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
[email protected]
.#...
.#...
@..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 }
时间: 2024-10-13 00:51:18

hdu 2612 多终点BFS的相关文章

BFS(最短路) HDU 2612 Find a way

题目传送门 1 /* 2 BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-4 19:36:36 7 File Name :HDOJ_2612.cpp 8 ************************************************

Problem N HDU 2612 Find a way (两次BFS求最值)

N - Find a way Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2612 Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei hav

HDU 2612 -Find a way (注意细节的BFS)

题目链接:Find a Way 题目不难,前几天做,当时准备写双向BFS的,后来处理细节上出了点问题,赶上点事搁置了,今天晚上重写的,没用双向,用了两次BFS搜索,和双向BFS 道理差不多,只是这题有个小坑,需要注意 1.Y不能经过M,M不能经过Y,也就是说有Y和M的格子,可以默认为是墙 2.必须是Y和M都能到达的KFC才行,只是其中一个到达不行 例如下列数据:答案既不是22 也不是 88 而是110,左下角的KFC满座条件 5 5 Y..#@ ...M. ....# ..... @.... 小

HDU 1072 Nightmare (BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意: 走迷宫,初始剩余时间为6min,每步1min:到reset区是若剩余时间大于0,则可以重置.到终点3区,若时间大于0,则成功逃脱.(可以走回路) 0:wall 1:可以走 2:起点 3:终点 4:剩余时间重置为6 源代码: #include<iostream> #include<cstring> #include<cstdio> #include<q

hdu 1242:Rescue(BFS广搜 + 优先队列)

Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Angel was caught by the MOLIGPY

hdu 1429 状压bfs

#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x3f3f3f3f #define ll __in

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

POJ 2243 || HDU 1372:Knight Moves(BFS)

Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11223 Accepted: 6331 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visit

hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)

Mines Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1110    Accepted Submission(s): 280 Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all peo