1. 程式人生 > >Find a way(兩個BFS)

Find a way(兩個BFS)

city any queue ret find 初始化 typedef 維數 opened

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
Y..@.
.#...
.#...
@..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)