Find a way(BFS)

本文介绍了一种通过广度优先搜索算法找到两个朋友从不同起点出发,在城市地图上寻找最短时间到达同一肯德基会面的方法。考虑到地图上的障碍物和可达路径,通过两次广度优先搜索分别计算两个起点到所有肯德基位置所需时间,最终确定总用时最短的会面地点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

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
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

题意:

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();
    }
}



The Kevin Bacon game is a popular trivia game that is played by connecting Hollywood actors to Kevin Bacon through their movies. The game has inspired various computer-based versions, including one that is implemented using the breadth-first search (BFS) algorithm in Python. Breadth-first search is a graph traversal algorithm that operates by exploring all the vertices at a given depth before moving on to the vertices at the next depth level. This makes it a perfect algorithm for traversing the relationship network of movie stars and linking them to Kevin Bacon. To implement the Kevin Bacon game using BFS in Python, the first step is to represent the relationships between the actors and movies as a graph. This can be done using an adjacency list or adjacency matrix representation. Once the graph is created, the BFS algorithm can be applied to traverse the graph and find the shortest path between two actors. The BFS algorithm starts with the starting actor – in this case, Kevin Bacon – and explores all the actors connected to him through the movies they have starred in. Then, it examines all the actors connected to these actors, and so on until the target actor is found. Along the way, the algorithm keeps track of the path taken to reach the target actor. The Python implementation of the Kevin Bacon game using BFS is a fun and interactive way to explore the relationships between Hollywood actors and their movies. It is also a great example of the versatile uses of graph traversal algorithms like BFS in solving real-world problems.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值