Find a way
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 48 Accepted Submission(s) : 15
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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.
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
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.
Sample Input
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
Sample Output
66 88 66
———————————————————————————————————————————————————
题目说两个人,他们想在某家KCF见面,要求他们到这家KCF的时间花费最少。
我们从每个kfc出发搜到每个人,选出到两个人距离和最小的就是答案
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
char mp[205][205];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int vir[205][205];
int m,n,ans1,ans2,mn;
struct node
{
int x,y,cnt;
};
bool cheak(int i,int j)
{
if(i<1||i>m||j<1||j>n||mp[i][j]=='#')
return 0;
else
return 1;
}
void bfs(int si,int sj,int yi,int yj,int mi,int mj)
{
queue<node>q;
node f,d;
memset(vir,0,sizeof(vir));
f.x=si;
f.y=sj;
f.cnt=0;
vir[f.x][f.y]=1;
q.push(f);
while(!q.empty())
{
f=q.front();
q.pop();
if(ans1==-1&&ans2==-1&&2*f.cnt>=mn)
return;
if(ans1==-1&&ans2!=-1&&f.cnt+ans2>=mn)
return;
if(ans1!=-1&&ans2==-1&&f.cnt+ans1>=mn)
return;
if(f.x==yi&&f.y==yj&&ans1==-1)
{
ans1=f.cnt;
}
if(f.x==mi&&f.y==mj&&ans2==-1)
{
ans2=f.cnt;
}
if(ans1!=-1&&ans2!=-1)
return;
for(int i=0;i<4;i++)
{
d.x=f.x+dir[i][0];
d.y=f.y+dir[i][1];
if(cheak(d.x,d.y)&&!vir[d.x][d.y])
{
d.cnt=f.cnt+1;
vir[d.x][d.y]=1;
q.push(d);
}
}
}
return ;
}
int main()
{
int yi,yj,mi,mj,ans;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='Y')
{
yi=i;
yj=j;
}
if(mp[i][j]=='M')
{
mi=i;
mj=j;
}
}
mn=100000;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
if(mp[i][j]=='@')
{
ans1=-1;
ans2=-1;
bfs(i,j,yi,yj,mi,mj);
if(ans1!=-1&&ans2!=-1)
{
ans=ans1+ans2;
if(ans<mn)
mn=ans;
}
}
}
printf("%d\n",mn*11);
}
return 0;
}