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

题意:

求分别从Y和M出发到达@用的总时间最少

解题思路:

两遍BFS,每次从其中一个起点出发,到达可到的点的时间,不能到达则记为无穷大,然后遍历每一个@,取 ans = min(ans, time1[i][j] + time2[i][j]);

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;
const int INF = 1e7 + 7;
const int N = 210;

char g[N][N];
int used[N][N];
int time1[N][N];
int time2[N][N];

int n, m;
struct node{
    int x, y;
    node(int i = 0, int j = 0) : x(i), y(j) {};
};

int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

queue<node> que;    
void bfs1(int x, int y)     // 从第一个起点出发记录到每点的时间
{
    memset(used, 0, sizeof(used));
    memset(time1, 0, sizeof(time1));
    while(!que.empty()) que.pop();

    que.push(node(x, y));
    used[x][y] = 1;
    time1[x][y] = 0;
    while( !que.empty())
    {
        node now = que.front(); que.pop();
        for(int i=0; i<4; i++)
        {
            int dx = now.x + d[i][0];
            int dy = now.y + d[i][1];
            if(dx >= 0 && dx < n && dy >= 0 && dy < m && g[dx][dy] != '#' && !used[dx][dy])
            {
                time1[dx][dy] = time1[now.x][now.y] + 1;    // 到下一步的时间加一
                used[dx][dy] = 1;
                que.push(node(dx, dy));
            }
        }
    }
    for(int i=0; i<n; i++)  // 不能到达的点记为无穷大
    {
        for(int j=0; j<m; j++)
        {
            if(time1[i][j] == 0)
                time1[i][j] = INF;
        }
    }


}

void bfs2(int x, int y)     // 类似BFS1
{
    memset(used, 0, sizeof(used));
    memset(time2, 0, sizeof(time2));
    while( !que.empty())    que.pop();
   
    que.push(node(x, y));
    used[x][y] = 1;
    time2[x][y] = 0;
    while(!que.empty())
    {
        
        node now = que.front(); que.pop();
        for(int i=0; i<4; i++)
        {
            int dx = now.x + d[i][0];
            int dy = now.y + d[i][1];
            if(dx >= 0 && dx < n && dy >= 0 && dy < m && g[dx][dy] != '#' && !used[dx][dy])
            {
                time2[dx][dy] = time2[now.x][now.y] + 1;
                used[dx][dy] = 1;
                que.push(node(dx, dy));
            }
        }
    }
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            if(time2[i][j] == 0)
                time2[i][j] = INF;
}


int main()
{
    while(scanf("%d %d", &n, &m) != EOF)
    {
        for(int i=0; i<n; i++)
            scanf("%s", g[i]);

        int x1, y1, x2, y2;
        for(int i=0; i<n; i++)  // 寻找两个起点
        {
            for(int j=0; j<m; j++)
            {
                if(g[i][j] == 'Y')
                     x1 = i, y1 = j;    
                else if(g[i][j] == 'M')
                    x2 = i, y2 = j;
            }
        }
        // printf("%d %d %d %d\n", x1, y1, x2, y2);
        bfs1(x1, y1);
        
        bfs2(x2, y2);
        

        int ans = INF;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(g[i][j] == '@')  // 每次取总时间最少的
                    ans = min(ans, time1[i][j] + time2[i][j]);
            }
        }
        printf("%d\n", ans * 11);
    }


    return 0;
}