题目大意:给出nXm的字符矩阵,有多个‘@’,一个‘Y’,一个‘M’,‘.’可以走,‘#’不可以走,求Y和M同时到一个@的最小步数*11
解题思路:搜索Y和M到每一个@
#include <bits/stdc++.h>
using namespace std;
const int maxn = 210;
const int INF = 0xfffffff;
const int dx[] = {-1,1,0,0};
const int dy[] = {0,0,1,-1};
char s[maxn][maxn];
int d[maxn][maxn][2],vis[maxn][maxn];
int flag,n,m;
struct Node
{
int x,y,step;
Node(int x,int y,int z):x(x),y(y),step(z){}
Node(){}
};
void bfs(int x,int y)
{
memset(vis,0,sizeof(vis));
queue<Node> q;
q.push(Node(x,y,0));
vis[x][y] = 1;
while(!q.empty())
{
Node cur = q.front();
q.pop();
for(int i = 0; i < 4; i++)
{
int xx = cur.x+dx[i];
int yy = cur.y+dy[i];
if(!vis[xx][yy] && xx>=1 && xx<=n && yy>=1 && yy<=m && s[xx][yy] != '#')
{
vis[xx][yy] = 1;
d[xx][yy][flag] = cur.step+1;
q.push(Node(xx,yy,cur.step+1));
}
}
}
}
int main()
{
while(scanf("%d%d%*c",&n,&m) != EOF)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
d[i][j][0] = d[i][j][1] = INF;
for(int i = 1; i <= n; i++)
scanf("%s",s[i]+1);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(s[i][j] == 'Y')
{
flag = 0;
bfs(i,j);
}
if(s[i][j] == 'M')
{
flag = 1;
bfs(i,j);
}
}
}
int ans = INF;
for(int i =1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(s[i][j] == '@')
ans = min(ans,d[i][j][0]+d[i][j][1]);
printf("%d\n",ans*11);
}
return 0;
}