H - Find a way(两次bfs)

本文介绍了一个关于寻找两个人到达同一地点最短总时间的问题,并通过使用广度优先搜索(BFS)算法解决这一问题。文章提供了完整的C++代码实现,展示了如何在一张地图上找到从两个不同起点到特定目标点的最短路径。

H - Find a way

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到达同一个’@’点的最短距离(注意算完步数之后,要*11)
bfs 两次,要注意在第二次开始的时候,要将标记数组清零~

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 267
char ma[MAXN][MAXN];//存图
int n, m;//图的大小
int v[MAXN][MAXN];//标记数组
struct node
{
    int x, y, step;//x坐标,y坐标,及到达此点的步数
}t, p, q, que[121212];
int ans[MAXN][MAXN];//记录到达@点的步数
int vectors[][2] = {1, 0, -1, 0, 0, 1, 0, -1};//上下左右四个方向
void bfs(int x, int y)
{
    v[x][y] = 1;//标记
    int front = 0, rear = 0;
    p.x = x;
    p.y = y;
    p.step = 0;
    que[rear++] = p;//入队
    while(front<rear)
    {
        q = que[front++];
        for(int i=0;i<4;i++)//四个方向遍历
         {
            p.x = q.x + vectors[i][0];
            p.y = q.y + vectors[i][1];
            if(p.x<0||p.x>=n||p.y<0||p.y>=m||v[p.x][p.y]||ma[p.x][p.y]=='#')
                 continue;//不符条件
            if(ma[p.x][p.y]=='@')//如果是终点
            {
                ans[p.x][p.y] += q.step + 1;//记录步数
            }
            v[p.x][p.y] = 1;//标记
            p.step = q.step+1;//更新步数
            que[rear++] = p;//入队
            }
    }
    return ;
}
int main()
{

    while(~scanf("%d %d", &n, &m))
    {
    memset(ans, 0, sizeof(ans));//清零
    for(int i=0;i<n;i++)
        scanf("%s", ma[i]);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(ma[i][j]=='Y')
            {
                t.x = i;//记录横坐标
                t.y = j;//记录竖坐标
                t.step = 0;
            }
        }
    }
    memset(v, 0, sizeof(v));
    bfs(t.x, t.y);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(ma[i][j]=='M')
            {
                t.x = i;
                t.y = j;
                t.step = 0;
            }
        }
    }
    memset(v, 0, sizeof(v));//记得清零
    bfs(t.x, t.y);
    int min = 989898;//定义一个大一点的数
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
              if(ans[i][j]<min&&ans[i][j])
                    min = ans[i][j];
        }
    }
    printf("%d\n", min*11);//别忘了*11
    }
    return 0;
}
本数据集来源于 2024 年 7 月在江西省中东部余干县、贵溪市、金溪县丘陵林地采集的千枚岩、红砂岩、花岗岩母质发育红壤关键带剖面土壤实测数据,空间覆盖 3 个县域不同岩性风化壳林地,采样点位经纬度分别为千枚岩剖面 P10(116.8316°E,28.5269°N)、红砂岩剖面 P08(117.1048°E,28.3492°N)、花岗岩剖面 P04(116.6883°E,27.9963°N);垂直空间采样深度存在差异,千枚岩与花岗岩剖面采样深度 0~600 cm,红砂岩剖面采样深度 0~450 cm,垂直分层采样分辨率为 0~50 cm 区间分 0~20 cm、20~50 cm 两层,50 cm 以下土层以 50 cm 为固定间隔分层,整套数据集共包含 36 条土壤剖面分层记录,其中 P10 千枚岩剖面 13 条、P08 红砂岩剖面 11 条、P04 花岗岩剖面 13 条。数据采集时间为 2024 年 7 月,实验室理化指标、矿物测试、酸碱滴定及统计建模工作于 2024 年 7 月 —2026 年 5 月完成,无时间序列连续监测数据,仅为单次野外剖面采样静态数据集。 数据集包含野外剖面基础信息、土壤酸碱滴定原始数据、土壤酸度指标、交换性盐基与交换性酸、土壤机械组成、有机质、黏土与原生矿物半定量 XRD 数据、无定形 / 晶形铁铝氧化物含量。全量理化指标计量单位统一规范:酸缓冲容量 pHBC 单位为 cmol・kg⁻¹・pH⁻¹,交换性酸、交换性盐基离子单位为 cmol・kg⁻¹,矿物以质量百分比(%)表示,、黏粒 / 粉粒 / 砂粒、有机质、铁铝氧化物单位均为g/kg,pH 为无量纲数值。 覆盖范围: 中位纬度: 28.2616 中位经度: 116.89654999999999 南界纬度: 27.9963 西界经度: 116.6883 北界纬度: 28.5269 东界经
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值