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.
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
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
66 88 66
题意:
Y和M要在KFC(在图中用@表示)约会,两人分别从各自的起点出发,每走一格花费11分钟,要求两人所花时间的最小值
思路:
由于是求最少时间,因此用广搜;
先用一次广搜把Y到达每一个KFC的时间记录下来,之后再用一次广搜,求M到达每一个KFC的时间,求出两个人的时间和,并更新最小值;
代码:
#include<stdio.h>
#include<string.h>
#include<queue>
void Bfs(int x,int y);
void Getmap();
using namespace std;
struct node{
int x,y,s;
};
int n,m;
int s1,e1,s2,e2; //s1,e1 Y的坐标,S2,e2是M的坐标
char map[250][250]; //存放地图
int book[250][250]; //记录在 M 访问过程中被访问的点
int K[250][250]; //记录 Y 到达每个KFC的时间
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
getchar();
memset(book,0,sizeof(book));
memset(K,0,sizeof(K));
Getmap();
int min=400000;
Bfs(s1,e1);
queue<node>p;
node temp;
temp.x=s2;
temp.y=e2;
temp.s=0;
book[s2][e2]=1;
p.push(temp);
int next[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int i;
while(!p.empty()){
temp=p.front();
for(i=0;i<4;i++){
int tx=temp.x+next[i][0];
int ty=temp.y+next[i][1];
if(tx<0||ty<0||tx==n||ty==m) //判断边界
continue;
if(map[tx][ty]=='@'&&book[tx][ty]==0){ //如果到达KFC 更新最小值 注意要保证此KFC不被访问过
int t=K[tx][ty]+temp.s+1;
// printf("T :%d\n",t);
min=min<t?min:t;
}
if(map[tx][ty]!='#'&&book[tx][ty]==0){
book[tx][ty]=1;
node tem;
tem.x=tx;
tem.y=ty;
tem.s=temp.s+1;
p.push(tem);
}
}
p.pop();
}
printf("%d\n",min*11);
}
return 0;
}
void Getmap(){
int i,j;
for(i=0;i<n;i++){
scanf("%s",map[i]);
for(j=0;j<m;j++){
/* if(map[i][j]=='@'){
printf("@ : %d %d\n",i,j);
}*/
if(map[i][j]=='Y'){ //记录Y的坐标
s1=i;e1=j;
}
if(map[i][j]=='M'){ //记录M的坐标
s2=i;e2=j;
}
}
}
}
void Bfs(int x,int y){
int next[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int Book[250][250]; //用来记录 Y 在访问过程中已经访问过的点
memset(Book,0,sizeof(Book));
queue<node>p;
node temp;
temp.x=x;
temp.y=y;
temp.s=0;
Book[x][y]=1;
p.push(temp);
int i;
while(!p.empty()){
temp=p.front();
for(i=0;i<4;i++){
int tx=temp.x+next[i][0];
int ty=temp.y+next[i][1];
if(tx<0||ty<0||tx==n||ty==m)
continue;
if(map[tx][ty]=='@'&&Book[tx][ty]==0){ //,每到达一个KFC 如果从未访问过,则记录下他的时间
K[tx][ty]=temp.s+1;
//printf("x: %d Y: %d time:%d\n",tx,ty,K[tx][ty]);
}
if(map[tx][ty]!='#'&&Book[tx][ty]==0){
Book[tx][ty]=1; //printf("Tx :%d Ty:%d\n",tx,ty);
node tem;
tem.x=tx;
tem.y=ty;
tem.s=temp.s+1;
p.push(tem);
}
}
p.pop();
}
}
本文介绍了一种通过广度优先搜索算法找到两个朋友从不同起点出发,在城市地图上寻找最短时间到达同一肯德基会面的方法。考虑到地图上的障碍物和可达路径,通过两次广度优先搜索分别计算两个起点到所有肯德基位置所需时间,最终确定总用时最短的会面地点。

582

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



