http://acm.hdu.edu.cn/showproblem.php?pid=2612
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4793 Accepted Submission(s): 1631
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. #...#
要特别注意要Y和M都能到达你当前判断的那个KFC;Y不走M所在的点且M不走Y所在的点和Y走M所在的点且M走Y所在的点 两种情况都能AC
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAX 400
#define INT_MAX 1000000
char graph[MAX][MAX];
struct KFC
{//每个KFC的坐标
int x,y;
}num[MAX];
int idy[MAX][MAX]/*yifenfei到图中各点的最短路*/,
idm[MAX][MAX]/*Merceki到图中各点的最短路*/,
xy,yy,//yifenfei的坐标
xm,ym;//Merceki的坐标
struct Rode
{
int x,y;
} s,p,q;
int dir[4][2]={
{-1,0},{1,0},{0,-1},{0,1}
};
int main()
{
int r,c,i,j,k;
while(scanf("%d %d",&r,&c)!=EOF)
{
for(i=0;i<r;i++)
scanf("%s",graph[i]);
for(i=0,k=0;i<r;i++)
for(j=0;j<c;j++)
{
if(graph[i][j]=='Y')
{
xy=i;yy=j;
}
if(graph[i][j]=='M')
{
xm=i;ym=j;
}
if(graph[i][j]=='@')
{
num[k].x=i;
num[k].y=j;
k+=1;
}
}
queue<Rode>Q;
memset(idy,0,sizeof(idy));
s.x=xy;
s.y=yy;
idy[s.x][s.y]=1;
Q.push(s);
while(!Q.empty())
{
q=Q.front();
Q.pop();
for(i=0;i<4;i++)
{
int X=q.x+dir[i][0];
int Y=q.y+dir[i][1];
if(X<0||X>=r||Y<0||Y>=c||graph[X][Y]=='#'||graph[X][Y]=='M'||idy[X][Y]!=0) continue;
idy[X][Y]=idy[q.x][q.y]+1;
p.x=X;p.y=Y;
Q.push(p);
}
}
memset(idm,0,sizeof(idm));
s.x=xm;
s.y=ym;
idm[s.x][s.y]=1;
Q.push(s);
while(!Q.empty())
{
q=Q.front();
Q.pop();
for(i=0;i<4;i++)
{
int X=q.x+dir[i][0];
int Y=q.y+dir[i][1];
if(X<0||X>=r||Y<0||Y>=c||graph[X][Y]=='#'||graph[X][Y]=='Y'||idm[X][Y]!=0) continue;
idm[X][Y]=idm[q.x][q.y]+1;
p.x=X;p.y=Y;
Q.push(p);
}
}
int min=INT_MAX;
for(i=0;i<k;i++)
{
//判断是否Y和M都可到达第i个KFC,因为少了这个判断wa了很多次
if(idy[num[i].x][num[i].y]==0||idm[num[i].x][num[i].y]==0) continue;
if(idy[num[i].x][num[i].y]+idm[num[i].x][num[i].y]<min)
min=idy[num[i].x][num[i].y]+idm[num[i].x][num[i].y];
}
printf("%d\n",(min-2)*11);
}
return 0;
}
寻找宁波最佳KFC会面地点

文章介绍了一项算法挑战,目的是帮助一对朋友在宁波选择一个合适的肯德基(KFC)会面地点,使他们从各自家中到会面地点的总时间最短。通过提供地图上初始位置和禁止通行区域的详细信息,算法能够计算出最优路径。
679

被折叠的 条评论
为什么被折叠?



