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

C++版本一

BFS

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define MAX 210
#define INF 0x3f3f3f
using namespace std;
int bu1[MAX][MAX];//记录第一个人步数
int bu2[MAX][MAX];//记录第二个人步数
int p;
char map[MAX][MAX];
int vis[MAX][MAX];
int n,m;
struct node
{
    int x,y;
    int step;
};
int MIN(int x,int y)
{
    return x<y?x:y;
}
void bfs(int x1,int y1,int p)
{
    memset(vis,0,sizeof(vis));
    int j,i,ok=0;
    int move[4][2]={0,1,0,-1,1,0,-1,0};
    queue<node>q;
    node beg,end;
    beg.x=x1;
    beg.y=y1;
    beg.step=0;
    q.push(beg);
    while(!q.empty())
    {
        end=q.front();
        q.pop();
        if(map[end.x][end.y]=='@')//遇见@则表示到达终点
        {
            if(p==1)
            bu1[end.x][end.y]=end.step;
            else
            bu2[end.x][end.y]=end.step;
        }
        for(i=0;i<4;i++)
        {
            beg.x=end.x+move[i][0];
            beg.y=end.y+move[i][1];
            if(!vis[beg.x][beg.y]&&0<=beg.x&&beg.x<n&&0<=beg.y&&beg.y<m&&map[beg.x][beg.y]!='#')
            {
                vis[beg.x][beg.y]=1;
                beg.step=end.step+11;
                q.push(beg);
            }
        }
    }
}
int main()
{
    int sum,j,i,t,k,x1,x2,y1,y2,min;
    int s[11000];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
         
        for(i=0;i<n;i++)
        {
            scanf("%s",map[i]);
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='Y')
                {
                    x1=i;y1=j;
                }
                else if(map[i][j]=='M')
                {
                    x2=i;y2=j;
                }
            }
        }
        memset(bu1,INF,sizeof(bu1));
        bfs(x1,y1,1);
        memset(bu2,INF,sizeof(bu2));
        bfs(x2,y2,2);
        min=INF;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(bu1[i][j]!=INF&&bu2[i][j]!=INF)
                {
                    min=MIN(bu1[i][j]+bu2[i][j],min);//取两者步数和的最小值
                }
            }
        }
        printf("%d\n",min);
    }
    return 0;     
}

C++版本二

BFS

#include<map>
#include<stack>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int INF=1e9;
typedef long long LL;
int n,m;
int w[204][204];
char area[204][204];
bool vis[204][204];
int d[4][2]= {{0,1},{0,-1},{-1,0},{1,0}};
struct pp
{
    int x,y,t;
} q1,q2,q3,q4;
bool check(int x,int y)
{
    if(vis[x][y]||area[x][y]=='#'||x<0||x>=n||y<0||y>=m)
        return 0;
    return 1;
}
void init()
{
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            w[i][j]=INF;
    mem(vis,0);
}
void bfs()
{
    int num=0,num1=0,num2=0;
    queue<pp> q;
    for(int i=0; i<n; i++)
    {
        scanf("%s",area[i]);
        for(int j=0; j<m; j++)
        {
            if(area[i][j]=='Y')
            {
                q1.x=i;
                q1.y=j;
                q1.t=0;
                vis[i][j]=1;
            }
            else if(area[i][j]=='M')
            {
                q3.x=i;
                q3.y=j;
                q3.t=0;
            }
            else if(area[i][j]=='@')
                num++;//剪枝。
        }
    }
    q.push(q1);
    while(!q.empty())
    {
        q1=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            q2.x=q1.x+d[i][0];
            q2.y=q1.y+d[i][1];
            if(check(q2.x,q2.y))
            {
                q2.t=q1.t+1;
                vis[q2.x][q2.y]=1;
                q.push(q2);
                if(area[q2.x][q2.y]=='@')
                {
                    num1++;
                    w[q2.x][q2.y]=q2.t;
                    if(num1==num)
                        break;
                }
            }
        }
    }
    while(!q.empty())
        q.pop();
    mem(vis,0);
    vis[q3.x][q3.y]=1;
    q.push(q3);
    while(!q.empty())
    {
        q3=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            q4.x=q3.x+d[i][0];
            q4.y=q3.y+d[i][1];
            if(check(q4.x,q4.y))
            {
                q4.t=q3.t+1;
                vis[q4.x][q4.y]=1;
                q.push(q4);
                if(area[q4.x][q4.y]=='@')
                {
                    num2++;
                    w[q4.x][q4.y]+=q4.t;
                    if(num2==num)
                        break;
                }
            }
        }
    }
}
void print()
{
    int minn=1e9;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(area[i][j]=='@')
            {
                if(minn>w[i][j])
                    minn=w[i][j];
            }
        }
    }
    printf("%d\n",minn*11);
}
int main()
{
    while(~scanf("%d%d%*c",&n,&m))
    {
        init();
        bfs();
        print();
    }
}

C++版本三

BFS(完全暴力)

很明显OLE

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>

using namespace std;
char Map[210][210];
char tmp[210];
int n,m,ans;
int yx,yy,mx,my;
int vis[210][210];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
struct node{
    int x,y;
    int step;

}front1,start,temp,end1;

int bfs(int x1,int y1,int x2,int y2){
    queue <node>Q;
    while(!Q.empty())
        Q.pop();
        start.x=x1;
        start.y=y1;
        start.step=0;
        vis[start.x][start.y]=1;
    Q.push(start);
    while(!Q.empty()){
            front1=Q.front();
            Q.pop();

            for(int i=0;i<4;i++){
                temp.x=front1.x+dir[i][0];
                temp.y=front1.y+dir[i][1];

                if(temp.x<0||temp.x>=n||temp.y<0||temp.x>=m||Map[temp.x][temp.y]=='#')  continue;

                if(vis[temp.x][temp.y]==0){

                    vis[temp.x][temp.y]=1;
                    temp.step=front1.step+1;
                    if(temp.x==x2&&temp.y==y2) return temp.step;
                    Q.push(temp);
                    //printf("%d\n",temp.step);
                }
            }

    }
    return -1;
}

int main()
{   while(scanf("%d%d",&n,&m)!=EOF){
        int i,j;
        for(i=0;i<n;i++){
            scanf("%s",tmp);
            for(j=0;j<m;j++){
                Map[i][j]=tmp[j];
                if(Map[i][j]=='Y'){
                    yx=i;
                    yy=j;
                }
                if(Map[i][j]=='M'){
                    mx=i;
                    my=j;
                }
            }
        }
        int flag=0;
        ans=0;
        int cnt0=0,cnt1=0,cnt=0;
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                if(Map[i][j]=='@'){
                    memset(vis,0,sizeof(vis));
                    cnt0=bfs(yx,yy,i,j);

                    if(cnt0>=0){
                        memset(vis,0,sizeof(vis));
                        cnt1=bfs(mx,my,i,j);
                    }


                    if (cnt0>=0&&cnt1>=0) {
                        cnt=cnt0+cnt1;

                        if(flag==0) {ans=cnt;}
                        else    {ans=min(cnt,ans);}
                        flag=1;
                    }
                }
            }
        }

        cout << ans*11 << endl;

}
    //cout << "Hello world!" << endl;
    return 0;
}