#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<math.h>
using namespace std;
#define N 300
int a[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int n,m,w[N][N],ww[N][N],www[N][N];
char str[N][N];
struct node
{
    int x,y,s;
};
void q(int x,int y)
{
    memset(w,0,sizeof(w));
    queue<node> Q;
    node q,p;
    q.x=x;
    q.y=y;
    q.s=0;
    Q.push(q);
    w[q.x][q.y]=1;
    while(Q.size())
    {
        p=Q.front();Q.pop();
        for(int i=0;i<4;i++)
        {
            q.x=p.x+a[i][0];
            q.y=p.y+a[i][1];
            q.s=p.s+1;
            if(q.x>=0&&q.x<n&&q.y>=0&&q.y<m&&!w[q.x][q.y]&&str[q.x][q.y]!='#')
            {
                w[q.x][q.y]=1;
                Q.push(q);
                ww[q.x][q.y]++;
                www[q.x][q.y]+=q.s;
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(ww,0,sizeof(ww));
        memset(www,0,sizeof(www));
        for(int i=0; i<n; i++)
            scanf("%s",str[i]);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(str[i][j]=='Y')
                    q(i,j);
                if(str[i][j]=='M')
                    q(i,j);
            }
        }
        int ans=1234567;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(str[i][j]=='@'&&ww[i][j]==2)
                {
                    ans=min(ans,www[i][j]);
                }
            }
        }
        printf("%d\n",ans*11);
    }
    return 0;
}

http://acm.hdu.edu.cn/showproblem.php?pid=2612

posted on 2016-07-13 17:49  云胡不喜。  阅读(175)  评论(0编辑  收藏  举报