HDU 2612 Find a way(两遍BFS)

本文介绍了一种解决双起点最短路径问题的算法,通过两遍BFS遍历地图,计算从两个不同起点到达所有可达点的时间,最终找出到达指定目标点(KFC)的最短总时间。算法首先从一个起点开始进行BFS,记录下到达每个点的时间,然后从另一个起点重复此过程。最后,遍历所有目标点,选取总时间最小的路径。

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

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

题意:

求分别从Y和M出发到达@用的总时间最少

解题思路:

两遍BFS,每次从其中一个起点出发,到达可到的点的时间,不能到达则记为无穷大,然后遍历每一个@,取 ans = min(ans, time1[i][j] + time2[i][j]);

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;
const int INF = 1e7 + 7;
const int N = 210;

char g[N][N];
int used[N][N];
int time1[N][N];
int time2[N][N];

int n, m;
struct node{
    int x, y;
    node(int i = 0, int j = 0) : x(i), y(j) {};
};

int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

queue<node> que;    
void bfs1(int x, int y)     // 从第一个起点出发记录到每点的时间
{
    memset(used, 0, sizeof(used));
    memset(time1, 0, sizeof(time1));
    while(!que.empty()) que.pop();

    que.push(node(x, y));
    used[x][y] = 1;
    time1[x][y] = 0;
    while( !que.empty())
    {
        node now = que.front(); que.pop();
        for(int i=0; i<4; i++)
        {
            int dx = now.x + d[i][0];
            int dy = now.y + d[i][1];
            if(dx >= 0 && dx < n && dy >= 0 && dy < m && g[dx][dy] != '#' && !used[dx][dy])
            {
                time1[dx][dy] = time1[now.x][now.y] + 1;    // 到下一步的时间加一
                used[dx][dy] = 1;
                que.push(node(dx, dy));
            }
        }
    }
    for(int i=0; i<n; i++)  // 不能到达的点记为无穷大
    {
        for(int j=0; j<m; j++)
        {
            if(time1[i][j] == 0)
                time1[i][j] = INF;
        }
    }


}

void bfs2(int x, int y)     // 类似BFS1
{
    memset(used, 0, sizeof(used));
    memset(time2, 0, sizeof(time2));
    while( !que.empty())    que.pop();
   
    que.push(node(x, y));
    used[x][y] = 1;
    time2[x][y] = 0;
    while(!que.empty())
    {
        
        node now = que.front(); que.pop();
        for(int i=0; i<4; i++)
        {
            int dx = now.x + d[i][0];
            int dy = now.y + d[i][1];
            if(dx >= 0 && dx < n && dy >= 0 && dy < m && g[dx][dy] != '#' && !used[dx][dy])
            {
                time2[dx][dy] = time2[now.x][now.y] + 1;
                used[dx][dy] = 1;
                que.push(node(dx, dy));
            }
        }
    }
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            if(time2[i][j] == 0)
                time2[i][j] = INF;
}


int main()
{
    while(scanf("%d %d", &n, &m) != EOF)
    {
        for(int i=0; i<n; i++)
            scanf("%s", g[i]);

        int x1, y1, x2, y2;
        for(int i=0; i<n; i++)  // 寻找两个起点
        {
            for(int j=0; j<m; j++)
            {
                if(g[i][j] == 'Y')
                     x1 = i, y1 = j;    
                else if(g[i][j] == 'M')
                    x2 = i, y2 = j;
            }
        }
        // printf("%d %d %d %d\n", x1, y1, x2, y2);
        bfs1(x1, y1);
        
        bfs2(x2, y2);
        

        int ans = INF;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(g[i][j] == '@')  // 每次取总时间最少的
                    ans = min(ans, time1[i][j] + time2[i][j]);
            }
        }
        printf("%d\n", ans * 11);
    }


    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值