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
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.
题意:求两个人到达一个同一个kfc的最短路程
思路:直接bfs两次搜出两个人分别到每个地方的路程,最后从每个kfc的位置求出最小的路程和
代码:
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <cctype>
#include <sstream>
#define INF 0x3f3f3f3f
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef long long LL;
using namespace std;
const int maxn = 2e2 + 5;
const int mod = 1e8 + 7;
int n,m;
char mp[maxn][maxn];
int to[4][2]={0,-1,0,1,1,0,-1,0};
bool vis[maxn][maxn],flag;
int ans1[maxn][maxn],ans2[maxn][maxn];
struct node{
int x,y;
int step;
}now,nex;
void bfs(int sx,int sy){
memset(vis,0,sizeof(vis));
queue<node>q;
now.x=sx;
now.y=sy;
now.step=0;
q.push(now);
vis[sx][sy]=1;
while (!q.empty()){
now=q.front();
q.pop();
for (int i=0;i<4;i++){
nex.x=now.x+to[i][0];
nex.y=now.y+to[i][1];
if (nex.x<0||nex.y<0||nex.x>=n||nex.y>=m) continue;
if (mp[nex.x][nex.y]=='#') continue;
if (vis[nex.x][nex.y]) continue;
vis[nex.x][nex.y]=1;
nex.step=now.step+1;
if (flag==0) {
ans1[nex.x][nex.y]=nex.step;
}
else {
ans2[nex.x][nex.y]=nex.step;
}
q.push(nex);
}
}
}
int main() {
//freopen ("in.txt", "r", stdin);
while (~scanf ("%d%d",&n,&m)){
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
cin>>mp[i][j];
ans1[i][j]=ans2[i][j]=INF;
}
}
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
if (mp[i][j]=='Y') {flag=0;bfs(i,j);}
if (mp[i][j]=='M') {flag=1;bfs(i,j);}
}
}
int ans=INF;
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
if (mp[i][j]=='@') ans=min(ans,ans1[i][j]+ans2[i][j]);
}
}
printf ("%d\n",ans*11);
}
return 0;
}