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

解题报告:起初我做这道题的时候我是想用一个dis数组来记录两个起点到kfc的距离,结果发现过不了,是因为dis数组无法记录起点是否能到该点,同时如果用每次搜到kfc 然后在bfs 会超时,不如分别bfs两个起点,把记录每个点到达@的距离。

#include<iostream>
#include<cstring>
#include<stdio.h>
#include<queue>
using namespace std;
const	int N=210;
char a[N][N];
int st[N][N];
int dis1[N][N];
int dis2[N][N];
int sx1,sy1,sx2,sy2;
int ans;
int n,m;
struct node{
   
	int x;
	int y;
	int step;
};	
int dx[4]={
   1,0,-1,0},dy[4]={
   0,1,0,-1}; 
void  bfs(int sx,int sy,int dis[][N])
{
   
	memset(st,0,sizeof st);
	queue<node>q1;
	q1.push({
   sx,sy,0});
	st[sx][sy]=true;
	while(q1.size())
	{
   
		node t=q1.front();
		q1.pop();
		if(a[t.x][t.y]=='@')
		{
   
			dis[t.x][t.y]=t.step;
		}
		for(int i=0;i<4;i++)
		{
   
			int tx=t.x+dx[i];
			int ty=t.y+dy[i];
			if(tx<0||ty<0||tx>=n||ty>=m)	continue;
			if(a[tx][ty]!='#'&&!st[tx][ty])
			{
   
				st[tx][ty]=1;
				q1.push({
   tx,ty,t.step+1});
			}
		}
	}
}
int main()
{
   
	while(scanf("%d%d",&n,&m)!=EOF)
	{
   
		ans=1e9;
		for(int i=0;i<n;i++)
		scanf("%s",a[i]);
		memset(st,0,sizeof st);
		for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		{
   
			if(a[i][j]=='Y')
			{
   
				sx1=i;
				sy1=j;
			}
			if(a[i][j]=='M')
			{
   
				sx2=i;
				sy2=j;
			}
		}
		memset(dis1,0x3f,sizeof dis1);
		memset(dis2,0x3f,sizeof dis2);
		bfs(sx1,sy1,dis1);
		bfs(sx2,sy2,dis2);
		for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		if(a[i][j]=='@')
		{
   
			if(dis1[i][j]!=0x3f3f3f3f&&dis2[i][j]!=0x3f3f3f3f)
			ans=min(ans,dis1[i][j]*11+dis2[i][j]*11);
		}
		cout<<ans<<endl;	
	}
	return 0;
}