Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10758 Accepted Submission(s): 3538
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
Author
yifenfei
Source
Recommend
#include<iostream>
#include<queue>
#include<string>
#include<cstring>
#include<cstdio>
#define inf 0x6ffffff
using namespace std;
int vis[250][250];
char map[250][250];
int dx[]={
1,-1,0,0
};
int dy[]={
0,0,1,-1
};
int s1,s2;
int s3,s4;
int flag[250][250];//用于记录步数
int flag2[250][250];
int n,m;
typedef pair<int,int> p;
void bfs(int sx,int sy,int a[250][250])
{
queue<p> que;
que.push(p(sx,sy));
vis[sx][sy]=1;
while(!que.empty())
{
p now=que.front();
que.pop();
int ex=now.first;
int ey=now.second;
for(int i=0;i<4;i++)
{
int x=ex+dx[i];
int y=ey+dy[i];
//cout<<x<<" "<<y<<endl;
if(x>=n||x<0||y>=m||y<0||vis[x][y]||map[x][y]=='#')
continue;
if(x<n&&m>y&&x>=0&&y>=0&&map[x][y]!='#'&&!vis[x][y])
{
vis[x][y]=1;
a[x][y]=a[ex][ey]+1;
}
que.push(p(x,y));
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;i++)
{
scanf("%s",map[i]);
for(int j=0;j<m;j++)
{
if(map[i][j]=='M')
{
s1=i;
s2=j;
}
if(map[i][j]=='Y')
{
s3=i;
s4=j;
}
}
}
memset(vis,0,sizeof(vis));
memset(flag,0,sizeof(flag));
memset(flag2,0,sizeof(flag2));
vis[s1][s2]=1;
bfs(s1,s2,flag);
memset(vis,0,sizeof(vis));//将第一个人的记录清楚
bfs(s3,s4,flag2);
int minn=inf;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
if(map[i][j]=='@'&&flag[i][j]&&flag2[i][j])//找到两个人可以到达的kfs 找出最短时间
{
minn=min(minn,flag[i][j]+flag2[i][j]);
}
}
printf("%d\n",minn*11);
}
return 0;
}