题目链接:传送门

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

题目大意:输入Y,M表示接下来有一个Y*M的矩阵,Y和M分别表示两个人的地点,@表示KFC,#表示墙,问两个人到达KFC的时间相加最短是多少。

坑点:有的KFC可能在墙内,不能到达。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int m[202][202];
int n[202][202];
int w[202][202];
char s[202][202];
int Y,M;
int x1,x2,y1,y2;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
    int a,b,c;
};
void bfs(int x,int y,int ss[][202])
{
    int i;
	node st,ed;
	queue<node>q;
	st.a=x;
	st.b=y;
	st.c=0;
	q.push(st);
	while(!q.empty())
	{
		st=q.front();
		q.pop();
		for(i=0;i<4;i++)
		{
			ed.a=st.a+dir[i][0];
			ed.b=st.b+dir[i][1];
			if(ed.a<0||ed.b<0||ed.a>=Y||ed.b>=M||s[ed.a][ed.b]=='#'||w[ed.a][ed.b])
                continue;
			ed.c=st.c+1;
			w[ed.a][ed.b]=1;
		    if(s[ed.a][ed.b]=='@')
			{
				ss[ed.a][ed.b]=ed.c;
			}
			q.push(ed);
		}
	}
}
int main()
{
    int a,b,c,d,e,f,g;
    while(cin>>Y>>M)
    {
        for(a=0;a<Y;a++)
            cin>>s[a];
        for(b=0;b<Y;b++)
        {
            for(c=0;c<M;c++)
            {
                if(s[b][c]=='Y')
                {
                    x1=b;
                    y1=c;
                }
                if(s[b][c]=='M')
                {
                    x2=b;
                    y2=c;
                }
            }
        }
        memset(n,0,sizeof(n));
        memset(m,0,sizeof(m));
        memset(w,0,sizeof(w));
        w[x1][y1]=1;
        bfs(x1,y1,n);
        memset(w,0,sizeof(w));
        w[x2][y2]=1;
        bfs(x2,y2,m);
        int min=0x6ffffff;
        for(d=0;d<Y;d++)
        {
            for(e=0;e<M;e++)
            {
                if(min>n[d][e]+m[d][e]&&n[d][e]&&m[d][e])
                    min=n[d][e]+m[d][e];
            }
        }
        cout<<min*11<<endl;
    }
    return 0;
}