Gym 101572E Emptying Baltic【优先队列+BFS】

本文介绍了一种基于广度优先搜索(BFS)的抽水机模拟算法,该算法用于计算在一个给定海拔高度矩阵中,从指定位置开始,水能流向哪些区域以及总共能抽取多少水量。通过使用优先队列来模拟水流动过程,文章提供了完整的AC代码实现。

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


题目大意:


给出一个N*M的矩阵,然后在点(x,y)处设立一个抽水机,每个点有一个数代表海拔高度,负数就有水,水往低处流,问能够抽多少水。


思路:


直接Bfs一下即可,模拟拓展出去的过程。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int a[505][505];
int vis[505][505];
int fx[8]={0,0,1,-1,1,-1,1,-1};
int fy[8]={1,-1,0,0,1,-1,-1,1};
struct node
{
    int x,y,val;
    bool friend operator <(node a,node b)
    {
        return a.val>b.val;
    }
}now,nex;
int n,m;
int sx,sy;
void Bfs()
{
    memset(vis,0,sizeof(vis));
    priority_queue<node>s;
    now.x=sx;now.y=sy;now.val=a[sx][sy];
    vis[sx][sy]=-a[sx][sy];s.push(now);
    while(!s.empty())
    {
        now=s.top();
        s.pop();
        for(int i=0;i<8;i++)
        {
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            nex.val=a[nex.x][nex.y];
            if(nex.x>=1&&nex.x<=n&&nex.y>=1&&nex.y<=m&&vis[nex.x][nex.y]==0)
            {
                vis[nex.x][nex.y]=1;
                a[nex.x][nex.y]=max(a[nex.x][nex.y],a[now.x][now.y]);
                s.push(nex);
            }
        }
    }
    __int64 output=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]>=0)continue;
            else output+=-a[i][j];
        }
    }
    printf("%I64d\n",output);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        scanf("%d%d",&sx,&sy);
        Bfs();
    }
}
/*
5 5
-8 -8 -8 -8 -8
-8 -1 -1 -1 -8
-8 -1 -2 -1 -8
-8 -1 -1 -1 -8
-8 -8 -8 -8 -8
*/








### 配置和使用ns-3与ns-3-gym在VSCode中的网络仿真 #### 安装依赖项 为了确保能够在VSCode中顺利运行ns-3以及ns-3-gym,需要先安装一系列必要的软件包。这包括但不限于CMake、Python3及其pip工具等基础构建组件。 对于Ubuntu 20.04而言,可以通过如下命令来完成这些前置条件的部署: ```bash sudo apt update && sudo apt install -y build-essential cmake python3-dev python3-pip git libboost-all-dev autoconf automake bison flex g++ gcc libffi-dev libgmp-dev libmpc-dev libmpfr-dev libtool make pkg-config texinfo zlib1g-dev qtbase5-dev libcwiid-dev wiimote-tools ``` 接着利用`pip3`安装额外所需的Python库文件,特别是针对ns-3-gym的支持部分: ```bash pip3 install gym numpy matplotlib pybind11==2.6.2 ``` #### 下载并编译ns-3源码 获取最新的ns-3版本(此处假设为v3.34),解压后进入对应目录执行编译操作。考虑到后续可能涉及到频繁修改测试的需求,建议采用调试模式(Debug)来进行编译工作以便于排查错误信息。 ```bash git clone https://github.com/nsnam/ns-3-dev.git ns-3.34 cd ns-3.34 ./waf configure --build-profile=debug ./waf ``` #### 整合ns-3-gym模块至ns-3项目内 下载ns-3-gym仓库,并将其放置到ns-3项目的顶层路径下作为子模块管理;随后按照官方文档指示调整配置参数使得两者能够协同运作[^1]。 ```bash git submodule add https://github.com/tkn-tub/ns3-gym.git src/gym-api ./waf clean ./waf configure --with-gym-api=./src/gym-api/ ./waf ``` #### 设置VSCode开发环境 打开Visual Studio Code编辑器,在目标工程根目录创建`.vscode/launch.json`用于定义启动选项,同时编写相应的任务脚本(`tasks.json`)辅助日常编码流程自动化处理。下面给出了一组基本样例供参考: **launch.json** ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/debug/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "compile" } ] } ``` **tasks.json** ```json { "version": "2.0.0", "tasks": [ { "label": "compile", "command": "./waf", "group": { "kind": "build", "isDefault": true }, "detail": "Compile the project using waf.", "problemMatcher": ["$gcc"], "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" } } ] } ``` 通过上述步骤即可实现在VSCode环境下对ns-3及ns-3-gym的有效支持,方便开发者开展基于强化学习算法驱动下的无线通信场景模拟实验研究工作[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值