Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11189 Accepted Submission(s): 3672
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
题意大致就是两个人约好在咖啡馆见面,然后为了节省时间吧(咱不说他懒),找个两个人都最近的,每走一个格需要11分钟吧,最后输出两人的总时间。
双向bfs,我这里是弄了个二维数组保存一下每个人到每个咖啡馆的距离,然后去加,由于一开始的数组保存全部赋初值0,所以等于0的说明没有咖啡馆,直接跳过,就可以ac了。我感觉还可以剪枝,但过了,就不剪了。
ac代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
int n,m,flag,Y[2],M[2],dis[205][205][2],vis[205][205];
char map[205][205];
int dir[4][2]={1,0,0,1,-1,0,0,-1};
struct node {
int x,y,ceng;
};
queue<node> Q;
void init()
{
while(!Q.empty())
{
Q.pop();
}
}
int judge(node next)
{
// printf("%d *** %d\n",n,m);
// printf("%d *** %d\n",next.x,next.y);
if(next.x>=0 && next.x<n && next.y>=0 && next.y<m)
{
//printf("***judge***\n");
return 1;
}
return 0;
}
void bfs(int *s)
{
node tmp;
int i,j;
tmp.x=s[0];
//printf("S.x=%d***\n",s[0]);
tmp.y=s[1];
//printf("S.y=%d***\n",s[1]);
tmp.ceng=0;
vis[tmp.x][tmp.y]=1;
Q.push(tmp);
while(!Q.empty())
{
tmp=Q.front();
Q.pop();
if(map[tmp.x][tmp.y]=='@')
{
//printf("***dis***\n");
dis[tmp.x][tmp.y][flag]=tmp.ceng;
vis[tmp.x][tmp.y]=1;
}
for(i=0;i<4;i++)
{
node next;
next.ceng=tmp.ceng+1;
next.x=tmp.x+dir[i][0];
next.y=tmp.y+dir[i][1];
if(!vis[next.x][next.y]&&map[next.x][next.y]!='#'&&judge(next))
{
//printf("#########\n");
Q.push(next);
vis[next.x][next.y]=1;
}
}
}
}
int main()
{
int i,j,min;
while(~scanf("%d%d",&n,&m))
{
for(i=0;i<n;i++)
{
getchar();
scanf("%s",map[i]);
for(j=0;j<m;j++)
{
if(map[i][j]=='Y')
{
Y[0]=i;
Y[1]=j;
}
else if(map[i][j]=='M')
{
M[0]=i;
M[1]=j;
}
}
}
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
flag=0;
init();
bfs(Y);
memset(vis,0,sizeof(vis));
flag=1;
init();
bfs(M);
min=2147483647;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if((dis[i][j][0]+dis[i][j][1])!=0&&min>=(dis[i][j][0]+dis[i][j][1]))
{
min=(dis[i][j][0]+dis[i][j][1]);
}
}
}
printf("%d\n",min*11);
}
return 0;
}