Find a way(两个BFS)

Posted Xenny

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Find a way(两个BFS)相关的知识,希望对你有一定的参考价值。

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.

SampleInput

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
[email protected]
.#...
.#...
@..M.
#...#

SampleOutput

66
88
66
题目大意就是说两个人要去同一个KFC碰头,问你最短的时间是多少,然后每走一步耗时11分钟。
没啥坑点,用二维数组记录步数就得了。
不过要注意,有些KFC可能走不过去,所以初始化的时候赋一个大值,找最小值,然后就是两次BFS就行了。

AC代码

技术分享图片
  1 #include <iostream>
  2 #include <string>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <sstream>
  6 #include <iomanip>
  7 #include <map>
  8 #include <stack>
  9 #include <deque>
 10 #include <queue>
 11 #include <vector>
 12 #include <set>
 13 #include <list>
 14 #include <cstring>
 15 #include <cctype>
 16 #include <algorithm>
 17 #include <iterator>
 18 #include <cmath>
 19 #include <bitset>
 20 #include <ctime>
 21 #include <fstream>
 22 #include <limits.h>
 23 #include <numeric>
 24 
 25 using namespace std;
 26 
 27 #define F first
 28 #define S second
 29 #define mian main
 30 #define ture true
 31 
 32 #define MAXN 1000000+5
 33 #define MOD 1000000007
 34 #define PI (acos(-1.0))
 35 #define EPS 1e-6
 36 #define MMT(s) memset(s, 0, sizeof s)
 37 typedef unsigned long long ull;
 38 typedef long long ll;
 39 typedef double db;
 40 typedef long double ldb;
 41 typedef stringstream sstm;
 42 const int INF = 0x3f3f3f3f;
 43 
 44 char mp[205][205];
 45 int vis[205][205][2];
 46 int fx[4][2] = {0,1,0,-1,-1,0,1,0};
 47 int n,m,z;
 48 
 49 bool check(int x,int y){
 50     if(vis[x][y][z] == MAXN && x >= 0 && x < n && y >= 0 && y < m && mp[x][y] != #)
 51         return true;
 52     return false;
 53 }
 54 
 55 void bfs(pair< int,int >s){
 56     vis[s.F][s.S][z] = 0;
 57     queue< pair< int,int > >q;
 58     q.push(s);
 59     while(!q.empty()){
 60         pair< int,int >tp = q.front();
 61         q.pop();
 62         for(int i = 0; i < 4; i++){
 63             int next_x = tp.F + fx[i][0];
 64             int next_y = tp.S + fx[i][1];
 65             if(check(next_x,next_y)){
 66                 vis[next_x][next_y][z] = vis[tp.F][tp.S][z] + 1;
 67                 q.push(make_pair(next_x,next_y));
 68             }
 69         }
 70     }
 71 }
 72 
 73 int main(){
 74     ios_base::sync_with_stdio(false);
 75     cout.tie(0);
 76     cin.tie(0);
 77 
 78     while(cin>>n>>m && n && m){
 79         MMT(mp);
 80         fill(&vis[0][0][0],&vis[0][0][0]+205*205*2,MAXN);
 81         for(int i = 0; i < n; i++){
 82             for(int j = 0; j < m; j++){
 83                 cin>>mp[i][j];
 84             }
 85         }
 86         for(int i = 0; i < n; i++)
 87             for(int j = 0; j < m; j++){
 88                 if(mp[i][j] == Y){
 89                     z = 0;
 90                     bfs(make_pair(i,j));
 91                 }
 92                 if(mp[i][j] == M){
 93                     z = 1;
 94                     bfs(make_pair(i,j));
 95                 }
 96             }
 97         int ans = MAXN;
 98         for(int i = 0; i < n; i++){
 99             for(int j = 0; j < m; j++){
100                 if(mp[i][j] == @){
101                     ans = min(ans,vis[i][j][0] + vis[i][j][1]);
102                     //11000055
103                 }
104             }
105         }
106         cout << ans*11 << endl;
107     }
108 
109     return 0;
110 }
View Code

 



以上是关于Find a way(两个BFS)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 2612 find a way 双BFS

Find a way (BFS)

Hdu 2612-Find a way(bfs)

hdoj2612 Find a way (bfs)

Find a Way (双bfs)

HDU - 2612 Find a way(BFS)