Find a way(两个BFS)

简介: Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally.

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

 



目录
相关文章
|
6月前
|
存储 机器学习/深度学习 算法
并查集(Union Find)
并查集(Union Find)
60 3
|
4天前
|
算法 数据安全/隐私保护
BFS(Breath First Search 广度优先搜索)
BFS(Breath First Search 广度优先搜索)
14 1
|
5月前
|
人工智能 算法 Java
深度优先搜索(Depth-First Search,DFS)是一种用于遍历或搜索树或图的算法。
深度优先搜索(Depth-First Search,DFS)是一种用于遍历或搜索树或图的算法。
|
12月前
|
机器人 Python
并查集(Union-Find)
并查集(Union-Find)是一种用于解决动态连通性问题的数据结构,它主要用于处理不相交的集合(Disjoint Sets)之间的合并和查询操作。并查集的主要优点是,它不需要比较相邻的元素,而是通过分配和收集元素来进行操作,从而在处理大量数据时非常高效。
69 8
Find The Multiple(dfs和bfs都可)
Find The Multiple(dfs和bfs都可)
26 0
|
算法
DFS and BFS
DFS and BFS
47 0
codeforces1244——D.Paint the Tree(dfs)
codeforces1244——D.Paint the Tree(dfs)
83 0
Leetcode --- 树的遍历(DFS/BFS)
Leetcode --- 树的遍历(DFS/BFS)
Leetcode --- 树的遍历(DFS/BFS)