# HDU - 2612 Find a way（BFS）

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.

InputThe 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.
‘@’ KCF
OutputFor 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

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <queue>
5
6 using namespace std;
7
8 typedef struct{
9     int i,j;
10     int step;
11 }loca;
12
13 int n,m;
14 char mm[205][205];//地图
15 int flag[205][205];//此位置是否待过
16 int step[205][205];//到达此处的步数，只有@时才赋值
17 int isarrival[205][205];//如果此处两个人都到达才可以
18 int yifeni=0,yifenj=0;//yifen位置
19 int meri=0,merj=0;//mer位置
20 queue<loca> q;
21 loca now,nextt;
23                 0,0,-1,1};
24
25 void go(int si,int sj){
26     now.i=si;
27     now.j=sj;
28     now.step=0;
29     flag[si][sj]=1;
30     q.push(now);
31     while(!q.empty()){
32         now=q.front(); q.pop();
33         if(mm[now.i][now.j]=='@'){
34             step[now.i][now.j]+=now.step;
35             isarrival[now.i][now.j]++;
36         }
37         flag[now.i][now.j]=1;
38         for(int i=0;i<4;i++){
41             nextt.step=now.step+1;
42             if(nextt.i<0||nextt.i>=n||nextt.j<0||nextt.j>m){
43                 continue;
44             }
45             if(flag[nextt.i][nextt.j]==0 && mm[nextt.i][nextt.j]!='#'){
46                 q.push(nextt);
47                 flag[nextt.i][nextt.j]=1;
48             }
49         }
50     }
51 }
52
53 int main()
54 {
55     while(~scanf("%d %d",&n,&m)){
56         memset(step,0,sizeof(step));
57         memset(isarrival,0,sizeof(isarrival));
58         memset(flag,0,sizeof(flag));
59         for(int i=0;i<n;i++){
60             getchar();
61             for(int j=0;j<m;j++){
62                 scanf("%c",&mm[i][j]);
63                 if(mm[i][j]=='Y'){
64                     yifeni=i; yifenj=j;
65                 }
66                 if(mm[i][j]=='M'){
67                     meri=i; merj=j;
68                 }
69             }
70         }
71         go(yifeni,yifenj);
72         memset(flag,0,sizeof(flag));
73         go(meri,merj);
74
75         int minn=0x3f3f3f3f;
76         for(int i=0;i<n;i++){
77             for(int j=0;j<m;j++){
78                 if(step[i][j]!=0&&isarrival[i][j]==2){
79                     int temp=step[i][j];
80                     if(temp<minn){
81                         minn=temp;
82                     }
83                 }
84             }
85         }
86         printf("%d\n",minn*11);
87     }
88     return 0;
89 }