Find a way
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
题意:输入长宽,然后输入地图,‘.’为路,' #'为墙,YM分别是两个人的起点,@是终点,终点有多个,求两个人到任意一个终点的时间之和最少为多少。每走一步11分钟。
标准广搜求最短路,加了一个储存时间的变量,在每个点存下两个人走到该点的最短时间之和,判断该点如果是一个终点就与min比较。
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
using namespace std;
int m,n;
char c[205][205];
bool map[205][205];
int kfc[205][205];
int b[4][2]= {1,0,0,1,-1,0,0,-1};
struct node
{
int i,j,t;
};
int main()
{
int i,j,min;
int y1,y2,m1,m2;
while(~scanf("%d%d",&m,&n))
{
getchar();
for(i=1; i<=m; i++)
{
for(j=1; j<=n; j++)
{
scanf("%c",&c[i][j]);
if(c[i][j]=='Y')
{
y1=i;
y2=j;
}
if(c[i][j]=='M')
{
m1=i;
m2=j;
}
}
getchar();
}
memset(map,0,sizeof(map));
memset(kfc,0,sizeof(kfc));
queue<node>q;
node front,rear;
front.i=y1;
front.j=y2;
front.t=0;
map[y1][y2]=1;
q.push(front);
while(!q.empty())
{
front=q.front();
q.pop();
for(i=0; i<4; i++)
{
rear.i=front.i+b[i][0];
rear.j=front.j+b[i][1];
if(rear.i>0&&rear.j>0&&rear.i<=m&&rear.j<=n&&c[rear.i][rear.j]!='#'&&map[rear.i][rear.j]==0)
{
rear.t=front.t+1;
q.push(rear);
map[rear.i][rear.j]=1;
kfc[rear.i][rear.j]+=rear.t;
}
}
}
memset(map,0,sizeof(map));
map[m1][m2]=1;
front.i=m1;
front.j=m2;
front.t=0;
q.push(front);
while(q.size())
{
front=q.front();
q.pop();
for(i=0; i<4; i++)
{
rear.i=front.i+b[i][0];
rear.j=front.j+b[i][1];
if(rear.i>0&&rear.j>0&&rear.i<=m&&rear.j<=n&&c[rear.i][rear.j]!='#'&&map[rear.i][rear.j]==0)
{
rear.t=front.t+1;
q.push(rear);
map[rear.i][rear.j]=1;
kfc[rear.i][rear.j]+=rear.t;
}
}
}
min=99999;
for(i=1; i<=m; i++)
for(j=1; j<=n; j++)
{
if(kfc[i][j]&&kfc[i][j]<min&&c[i][j]=='@')
min=kfc[i][j];
}
printf("%d\n",min*11);
}
return 0;
}