到不了的话,那就无限时间喽
#include<stdio.h>
#include<string.h>
int n,m;
struct node
{
int x;
int y;
}Y,M,kfc[40010];
char map[210][210];
int queue[100010];
int way[210][210];
int kfctime[40010];
int move[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
int min(int g,int h)
{
return g>h?h:g;
}
void bfs(int x,int y)
{
int front=0;
int end=0;
queue[end++]=x;
queue[end++]=y;
memset(way,-1,sizeof(way));
way[x][y]=0;
while(front!=end)
{
int oldx=queue[front++];
int oldy=queue[front++];
for(int i=0;i<4;i++)
{
int newx=oldx+move[i][0];
int newy=oldy+move[i][1];
if(newx>=0&&newx<n&&newy>=0&&newy<m&&map[newx][newy]!='#'&&way[newx][newy]==-1)
{
way[newx][newy]=way[oldx][oldy]+1;
queue[end++]=newx;
queue[end++]=newy;
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int cin=0;
int i,j;
for(i=0;i<n;i++)
{
scanf("%s",map[i]);
for(j=0;j<m;j++)
{
if(map[i][j]=='Y')
{
Y.x=i;
Y.y=j;
}
if(map[i][j]=='M')
{
M.x=i;
M.y=j;
}
if(map[i][j]=='@')
{
kfc[cin].x=i;
kfc[cin].y=j;
cin++;
}
}
}
bfs(Y.x,Y.y);
memset(kfctime,0,sizeof(kfctime));
for(i=0;i<cin;i++)
{
if(way[kfc[i].x][kfc[i].y]==-1)
kfctime[i]=0x3f3f3f3f;
else
kfctime[i]=way[kfc[i].x][kfc[i].y];
}
bfs(M.x,M.y);
int ans=0x3f3f3f3f;
for(i=0;i<cin;i++)
{
if(way[kfc[i].x][kfc[i].y]!=-1)
{
ans=min(ans,kfctime[i]+way[kfc[i].x][kfc[i].y]);
}
}
printf("%d\n",11*ans);
}
return 0;
}