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题目,我们两个点同时跑一遍BFS,然后最后再枚举一下KFC的位置取最小值就是答案,这个题目是一个很好学习BFS的题目,不过有一个小错误我找了两个多小时都没找出来,后面才发现是初始化放错位置了,因为这个题目是要在main函数里面跑两次BFS但是我只在main函数里面初始化了一次vis数组,这就导致1最后的结果是错的,所以要把初始化代码放在BFS里面就能很好的解决问题,我估计这个错误我能记两年,估计也没谁会犯我这个错误吧~~

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
int b1[maxn][maxn], b2[maxn][maxn];
int vis[maxn][maxn];
char c[maxn][maxn];
const int inf = 0x3f3f3f3f;
int n, m;
int d[4][2] = {
   -1, 0, 1, 0, 0, -1, 0, 1};
struct node
{
   
    int x, y, step;
};
void bfs(int x, int y, int p)
{
   
    memset(vis, 0, sizeof(vis));
    queue<node> q;
    node s, e;
    s.x = x;
    s.y = y;
    s.step = 0;
    q.push(s);
    vis[s.x][s.y] = 1;
    while (!q.empty())
    {
   
        s = q.front();
        q.pop();
        if (c[s.x][s.y] == '@')
        {
   
            if (p == 1)
                b1[s.x][s.y] = s.step;
            else
                b2[s.x][s.y] = s.step;
        }
        for (int i = 0; i < 4; ++i)
        {
   
            e.x = s.x + d[i][0];
            e.y = s.y + d[i][1];
            if (e.x >= 1 && e.x <= n && e.y >= 1 && e.y <= m && c[e.x][e.y] != '#' && !vis[e.x][e.y])
            {
   
                vis[e.x][e.y] = 1;
                e.step = s.step + 11;
                q.push(e);
            }
        }
    }
}
int main()
{
   
    int x1, y1, x2, y2, ans;
    while (~scanf("%d%d", &n, &m))
    {
   
        memset(b1, inf, sizeof(b1));
        memset(b2, inf, sizeof(b2));
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j)
                scanf(" %c", &c[i][j]);
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j)
            {
   
                if (c[i][j] == 'Y')
                {
   
                    x1 = i;
                    y1 = j;
                }
                if (c[i][j] == 'M')
                {
   
                    x2 = i;
                    y2 = j;
                }
            }
        bfs(x1, y1, 1);
        bfs(x2, y2, 2);
        ans = inf;
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j)
            {
   
                if (b1[i][j] != inf && b2[i][j] != inf)
                    ans = min(ans, b1[i][j] + b2[i][j]);
            }
        printf("%d\n", ans);
    }
}
时间最迷人的魅力,就是让你坚定的成为自己。在生活的波澜起伏中成长,我们方才发现,人生,成为自己就好