杭电2612

本文介绍了一个关于双起点BFS寻路算法的问题,主要讲述了如何在一个包含多个障碍物的地图上找到两个人到最近的同一目标地点(如KFC)的最短总时间。通过从两个起点分别进行广度优先搜索,避免了重复路径计算,有效提高了算法效率。

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

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.
 

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
 


搜索

第一次看到这道题以为很难就先放下了今天又来写

写了一发bfs超时了

先贴下代码、

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,vis[201][201],d[4][2]={1,0,-1,0,0,1,0,-1};
char mm[201][201];
int vj(int x,int y){
    if(x<0||x>=n||y<0||y>=m||vis[x][y])return 0;
    return 1;
}
struct node {
    int x,y,t;
}k[40000],a,b;
int main(){
    while(cin>>n>>m){
        for(int i=0;i<n;i++)
            scanf("%s",mm[i]);
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(mm[i][j]=='Y'){
                    a.x=i;
                    a.y=j;
                    a.t=0;
                }
            }
        }
        memset(vis,0,sizeof(vis));
        queue<node>q;
        q.push(a);
        int len=0;
        while(!q.empty()){
            a=q.front();
            q.pop();
            if(mm[a.x][a.y]=='@'){
                k[len++]=a;
            }
            for(int i=0;i<4;i++){
                int xx=a.x+d[i][0];
                int yy=a.y+d[i][1];
                if(vj(xx,yy)&&mm[xx][yy]!='#'){
                    b.x=xx;
                    b.y=yy;
                    b.t=a.t+11;
                    vis[xx][yy]=1;
                    q.push(b);
                }
            }
        }
        int t[40001];
        int h=0;
        for(int i=0;i<len;i++){
            memset(vis,0,sizeof(vis));
            q.push(k[i]);
            while(!q.empty()){
                a=q.front();
                q.pop();
                if(mm[a.x][a.y]=='M'){
                    t[h++]=a.t;
                    break;
                }
                for(int i=0;i<4;i++){
                    int xx=a.x+d[i][0];
                    int yy=a.y+d[i][1];
                    if(vj(xx,yy)&&mm[xx][yy]!='#'){
                        b.x=xx;
                        b.y=yy;
                        b.t=a.t+11;
                        vis[xx][yy]=1;
                        q.push(b);
                    }
                }
            }
        }
        sort(t,t+h);
        cout<<t[0]<<endl;
    }
    return 0;
}

交了之后我都觉得自己蠢

第一次思路是从一个点去找肯德基,然后再从肯德基找另一个人

当肯德基数量跟多的时候就会超时

这样走了无数次重复的路


正解是从两个点搜索两次


#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,vis[201][201],t[200][200],d[4][2]={1,0,-1,0,0,1,0,-1};
char mm[201][201];
int vj(int x,int y){
    if(x<0||x>=n||y<0||y>=m||vis[x][y])return 0;
    return 1;
}
struct node {
    int x,y,t;
}k[40000],a,b,c;
int main(){
    while(cin>>n>>m){
        for(int i=0;i<n;i++)
            scanf("%s",mm[i]);
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                t[i][j]=99999999;
                if(mm[i][j]=='Y'){
                    a.x=i;
                    a.y=j;
                    a.t=0;
                }
                if(mm[i][j]=='M'){
                    c.x=i;
                    c.y=j;
                    c.t=0;
                }
            }
        }
        memset(vis,0,sizeof(vis));
        queue<node>q;
        q.push(a);
        while(!q.empty()){
            a=q.front();
            q.pop();
            if(mm[a.x][a.y]=='@'){
                t[a.x][a.y]=a.t;
            }
            for(int i=0;i<4;i++){
                int xx=a.x+d[i][0];
                int yy=a.y+d[i][1];
                if(vj(xx,yy)&&mm[xx][yy]!='#'){
                    b.x=xx;
                    b.y=yy;
                    b.t=a.t+11;
                    vis[xx][yy]=1;
                    q.push(b);
                }
            }
        }
        memset(vis,0,sizeof(vis));
        q.push(c);
        while(!q.empty()){
            a=q.front();
            q.pop();
            if(mm[a.x][a.y]=='@'){
                t[a.x][a.y]+=a.t;
            }
            for(int i=0;i<4;i++){
                int xx=a.x+d[i][0];
                int yy=a.y+d[i][1];
                if(vj(xx,yy)&&mm[xx][yy]!='#'){
                    b.x=xx;
                    b.y=yy;
                    b.t=a.t+11;
                    vis[xx][yy]=1;
                    q.push(b);
                }
            }
        }
        int minn=99999999;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(t[i][j]<minn)minn=t[i][j];
            }
        }
        cout<<minn<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值