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.
题目大意:
终于在杭州度过了一年的学习,一飞回到家乡宁波。离开宁波一年了,亦芬菲有很多人要见面。特别是一个好朋友梅尔斯基。
一奋飞的家在乡下,梅尔斯基的家在市中心。于是一奋飞和奔驰安排在肯德基见面。在宁波有很多肯德基,他们想选择一个让总时间最小的。
现在给你一张宁波地图,奔驰和易芬飞都可以上下左右移动到相邻的路上,只要11分钟。
输入
输入包含多个测试用例。
每个测试用例包括,前两个整数n,m (2<=n,m<=200)。
接下来n行,每一行包含m个字符。
'Y’表示yifenfei的初始位置。
'M’表示Merceki的初始位置。
'#'表示不可通行;
'.'表示道路。
'@表示’KCF
#include<bits/stdc++.h>
using namespace std;
int b,c,v[210][210],ans[210][210],dx[4]={-1,0,1,0},dy[4]={0,1,0,-1},f[210][210];
char a[210][210];
struct node{
int x;
int y;
};
void bfs(int x,int y){
memset(v,0,sizeof v);
memset(f,0,sizeof f);
queue<node> q;
q.push({x,y});
v[x][y]=0;
f[x][y]=1;
while(!q.empty()){
node t=q.front();
q.pop();
if(a[t.x][t.y]=='@'){
ans[t.x][t.y]+=v[t.x][t.y];
}
for(int i=0;i<4;i++){
int tx=dx[i]+t.x;
int ty=dy[i]+t.y;
if(tx>=1&&tx<=b&&ty>=1&&ty<=c&&a[tx][ty]!='#'&&f[tx][ty]==0){
f[tx][ty]=1;
q.push({tx,ty});
v[tx][ty]=v[t.x][t.y]+11;
}
}
}
}
int main(){
while(cin>>b>>c){
memset(ans,0,sizeof ans);
int sx1,sy1,sx2,sy2;
for(int i=1;i<=b;i++)
for(int j=1;j<=c;j++){
cin>>a[i][j];
if(a[i][j]=='Y'){
sx1=i;
sy1=j;
}
if(a[i][j]=='M'){
sx2=i;
sy2=j;
}
}
bfs(sx1,sy1);
bfs(sx2,sy2);
int minn=40005;
for(int i=1;i<=b;i++)
for(int j=1;j<=c;j++){
if(a[i][j]=='@'&&ans[i][j]!=0){
minn=min(minn,ans[i][j]);
}
}
printf("%d\n",minn);
}
return 0;
}