BFS每日刷题

article/2025/6/30 13:52:51

 

目录

P1332 血色先锋队

173. 矩阵距离

P1162 填涂颜色

P1506 拯救oibh总部

P2895 [USACO08FEB] Meteor Shower S

P3395 路障 


P1332 血色先锋队

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int dis[600][600];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int n, m, a, b;
queue<pair<int, int>> q;
void bfs()
{while (!q.empty()){auto t = q.front();q.pop();for (int i = 0; i < 4; i++){int nx = t.first + dx[i];int ny = t.second + dy[i];if (nx >= 1 && ny >= 1 && nx <= n && ny <= m && dis[nx][ny] == -1){q.push({nx, ny});dis[nx][ny] = dis[t.first][t.second] + 1;}}}
}
int main()
{cin >> n >> m;cin >> a >> b;memset(dis, -1, sizeof dis);while (a--){int x, y;cin >> x >> y;dis[x][y] = 0;q.push({x, y});}bfs();while (b--){int x, y;cin >> x >> y;cout << dis[x][y] << endl;}return 0;
}

173. 矩阵距离

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int dis[1010][1010];
char s[1010][1010];
int n, m;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
queue<pair<int, int>> q;
void bfs()
{while (!q.empty()){auto t = q.front();q.pop();for (int i = 0; i < 4; i++){int nx = t.first + dx[i];int ny = t.second + dy[i];if (nx >= 1 && ny >= 1 && nx <= n && ny <= m && dis[nx][ny] == -1){q.push({nx, ny});dis[nx][ny] = dis[t.first][t.second] + 1;}}}
}
int main()
{cin >> n >> m;memset(dis, -1, sizeof dis);for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){cin >> s[i][j];if (s[i][j] == '1'){dis[i][j] = 0;q.push({i, j});}}}bfs();for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){cout << dis[i][j] << " ";}cout << endl;}return 0;
}

 

P1162 填涂颜色

#include <iostream>
#include <queue>
using namespace std;
bool vis[50][50];
int a[50][50];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int n;
queue<pair<int, int>> q;
void bfs(int x, int y)
{q.push({x, y});while (!q.empty()){auto t = q.front();q.pop();for (int i = 0; i < 4; i++){int nx = t.first + dx[i];int ny = t.second + dy[i];if (nx >= 0 && ny >= 0 && nx <= n + 1 && ny <= n + 1 && a[nx][ny] == 0 && !vis[nx][ny]){vis[nx][ny] = true;q.push({nx, ny});}}}
}
int main()
{cin >> n;for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){cin >> a[i][j];}}bfs(0, 0);for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){if (vis[i][j] == false && a[i][j] == 0){a[i][j] = 2;}cout << a[i][j] << " ";}cout << endl;}return 0;
}

P1506 拯救oibh总部

#include <iostream>
#include <queue>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
queue<pair<int, int>> q;
bool vis[510][510];
char s[510][510];
int n, m;
int res;
void bfs(int x, int y)
{q.push({x, y});while (!q.empty()){auto t = q.front();q.pop();for (int i = 0; i < 4; i++){int nx = t.first + dx[i];int ny = t.second + dy[i];if (nx >= 0 && ny >= 0 && nx <= n + 1 && ny <= m + 1 && s[nx][ny] != '*' && !vis[nx][ny]){vis[nx][ny] = true;q.push({nx, ny});}}}
}
int main()
{cin >> n >> m;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){cin >> s[i][j];}}bfs(0, 0);for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){if (s[i][j] == '0' && vis[i][j] == false){res++;}}}cout << res;return 0;
}

P2895 [USACO08FEB] Meteor Shower S

#include <iostream>
#include <queue>
#include <climits>
#include <cstring>
using namespace std;
int fire[310][310];
int dis[310][310];
int dx[] = {1, 0, -1, 0, 0};
int dy[] = {0, -1, 0, 1, 0};
int m;
queue<pair<int, int>> q;
int bfs()
{memset(dis, -1, sizeof dis);q.push({0, 0});dis[0][0] = 0;while (!q.empty()){auto t = q.front();q.pop();for (int i = 0; i < 4; i++){int nx = t.first + dx[i];int ny = t.second + dy[i];int nt = dis[t.first][t.second] + 1;if (nx >= 0 && ny >= 0 && nx <= 301 && ny <= 301 && dis[nx][ny] == -1){if (nt < fire[nx][ny]){q.push({nx, ny});dis[nx][ny] = nt;if (fire[nx][ny] == INT_MAX){return dis[nx][ny];}}}}}return -1;
}
int main()
{cin >> m;for (int i = 0; i <= 301; i++){for (int j = 0; j <= 301; j++){fire[i][j] = INT_MAX;}}while (m--){int x, y, t;cin >> x >> y >> t;for (int i = 0; i < 5; i++){int nx = x + dx[i];int ny = y + dy[i];if (nx >= 0 && ny >= 0 && nx <= 301 && ny <= 301 && fire[nx][ny] > t){fire[nx][ny] = t;}}}int res = bfs();cout << res;return 0;
}

P3395 路障 

#include <iostream>
#include <cstring>
#include <queue>
#include <climits>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int fire[1010][1010];
int dis[1010][1010];
int n;
bool bfs()
{queue<pair<int, int>> q; // 队列不能设置为全局变量,因为有多组数据memset(dis, -1, sizeof dis);q.push({1, 1});dis[1][1] = 0;// 注意n有可能等于1if (n == 1){return true;}while (!q.empty()){auto t = q.front();q.pop();for (int i = 0; i < 4; i++){int nx = t.first + dx[i];int ny = t.second + dy[i];int nt = dis[t.first][t.second] + 1;if (nx >= 1 && ny >= 1 && nx <= n && ny <= n && nt <= fire[nx][ny] && dis[nx][ny] == -1){q.push({nx, ny});dis[nx][ny] = nt;if (nx == n && ny == n){return true;}}}}return false;
}
void solve()
{cin >> n;for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){fire[i][j] = INT_MAX;}}int t = 1;for (int i = 1; i <= n * 2 - 2; i++){int x, y;cin >> x >> y;fire[x][y] = min(fire[x][y], t++);}bool res = bfs();if (!res){cout << "No" << endl;}else{cout << "Yes" << endl;}
}
int main()
{int t;cin >> t;while (t--){solve();}return 0;
}

 


http://www.hkcw.cn/article/cBoXIGFWrE.shtml

相关文章

中国城市规模指数(1992-2023)

1816 中国城市规模指数(1992-2023) 数据简介 中国城市规模指数&#xff0c;参考丁从明等&#xff08;2020&#xff09;的做法&#xff0c;通过中国夜间灯光数据&#xff0c;提取其中各城市夜间灯光值大于等于22的区域取其平均值&#xff0c;再取其自然对数即为城市规模指数数…

基于贝叶斯优化神经网络的光伏功率预测综述

基于贝叶斯优化神经网络的光伏功率预测综述 一、贝叶斯优化的基本原理与核心组件 贝叶斯优化&#xff08;Bayesian Optimization, BO&#xff09;是一种基于概率模型的全局优化方法&#xff0c;特别适用于高成本评估的黑盒函数优化问题。其核心由代理模型和采集函数构成&…

【Zephyr 系列 4】串口通信进阶:打造自己的 AT 命令框架

&#x1f9e0;关键词&#xff1a;Zephyr、UART、串口通信、AT命令、Shell、RTOS &#x1f4cc;适合人群&#xff1a;希望开发设备控制协议、调试接口、CLI 命令的嵌入式开发者 &#x1f3af; 本篇目标 使用 Zephyr 提供的 UART API 与 Shell 模块 实现一套可扩展的 ATCMD 风格…

Docker 镜像原理

目录 操作系统基础 Union FS(联合文件系统) 再看 Docker 镜像是什么 镜像实现原理 docker 镜像加载原理 docker 是操作系统层的虚拟化&#xff0c;所以 docker 镜像的本质是在模拟操作系统。我们先看下操作系统是什么。 操作系统基础 操作系统由&#xff1a;进程调度子系统、…

仓颉语言---Socket编程

一、什么是Socket编程&#xff1f; 1.定义 Socket&#xff08;套接字&#xff09;可以被理解为网络上两个进程之间通信的端点。它是网络通信的抽象表示&#xff0c;封装了底层网络协议的复杂性&#xff0c;为应用程序提供了一个简单统一的接口。 Socket 编程是一种网络编程范式…

格密码-LWE问题

格密码是一种备受关注的 PQC 算法&#xff0c;其安全性基于最坏情况下格问题的困难性&#xff0c;它是来自于 Regev 密码系统和 Lyubashevsky-Peikert-Regev 密码系统的思想。 2022 年&#xff0c;NIST 完成了 PQC 第三轮标准化过程&#xff0c;共有四种候选算法被选中进行标准…

力扣刷题Day 67:N 皇后(51)

1.题目描述 2.思路 方法1&#xff08;自己写的传统型回溯&#xff09;&#xff1a;将第一行的皇后依次放置在0 ~ n - 1上&#xff0c;可以发现0 ~ (⌊n / 2⌋ - 1)上的放置方案与⌈n / 2⌉ ~ (n - 1)上的放置方案是对称的&#xff0c;此外&#xff0c;如果n是奇数的话还需额外…

超级 AI 助手进阶攻略:Reflection 模式,开启 Agent 智能飞跃之旅

反思之言&#xff1a; 你有没有注意到&#xff0c;同样是AI&#xff0c;有些能帮你写代码、做决策&#xff0c;甚至聊人生&#xff1b;而有些却连基本的问题都答不对&#xff1f;这背后其实有一个关键差异&#xff1a;它会不会“反思”自己。 所谓Reflection&#xff08;反思…

[RoarCTF 2019]Easy Calc

查看源代码 <!--Ive set up WAF to ensure security.--> <script>$(#calc).submit(function(){$.ajax({url:"calc.php?num"encodeURIComponent($("#content").val()),type:GET,success:function(data){$("#result").html(<div …

【LLM】AI Agents vs. Agentic AI(概念应用挑战)

note AI Agent 已经可以自动感知环境、拆解任务、并灵活应对变化&#xff1b;与此同时&#xff0c;Agentic AI 又一次将“协作”提到新高度&#xff0c;让多个小团队般的 Agent 分工协作&#xff0c;共同实现“更高层次的目标”AI Agents 将在五个关键领域实现突破&#xff1a…

UE5 2D地图曝光太亮怎么修改

UE5 2D地图曝光怎么修改 在场景添加后期处理体积 修改后期处理体积Exposure曝光参数最大值最小值都改为0 勾选Infinite Extend 全地图范围应用此后期处理体积

pikachu通关教程-File Inclusion

文件包含漏洞 本地文件包含 http://127.0.0.1:1000/pikachu/vul/fileinclude/fi_local.php?filenamefile1.php&submit%E6%8F%90%E4%BA%A4%E6%9F%A5%E8%AF%A2 首先我们把file1改成file2&#xff0c;发现切换成功 那我们可不可以上传本地文件呢&#xff0c;答案是肯定的&a…

树莓派实验

一、在树莓派上完成驱动程序控制的 PWM LED灯。 1.PWM概述 PWM&#xff08;Pulse Width Modulation&#xff0c;脉宽调制&#xff09; 是一种通过调节信号脉冲宽度来模拟不同幅度模拟信号的技术。它通过周期性地改变信号的占空比&#xff08;即在一个信号周期内&#xff0c;高…

【HarmonyOS 5】鸿蒙应用实现发票扫描、文档扫描输出PDF图片或者表格的功能

【HarmonyOS 5】鸿蒙应用实现发票扫描、文档扫描输出PDF图片或者表格的功能 一、前言 图(1-1) HarmonyOS 的 ** 文档扫描控件(DocumentScanner)** 是 AI Vision Kit 提供的核心场景化视觉服务,旨在帮助开发者快速实现移动端文档数字化功能。 其核心能力包括:扫描合同、…

volatile,synchronized,原子操作实现原理,缓存一致性协议

文章目录 缓存一致性协议&#xff08;MESI&#xff09;volatile1. volatile 的作用2.volatile的底层实现3,volatile 实现单例模式的双重锁&#xff08;面手写&#xff09; synchronized1,基本用法2,可重入性3,Java对象头4,实现原理&#xff08;1&#xff09;代码块同步的实现&a…

Arch安装botw-save-state

devkitPro https://blog.csdn.net/qq_39942341/article/details/148387077?spm1001.2014.3001.5501 cargo https://blog.csdn.net/qq_39942341/article/details/148387783?spm1001.2014.3001.5501 megaton https://blog.csdn.net/qq_39942341/article/details/148388164?spm…

15-2021剑侠情缘2-各种修复完善+虚拟机单机端+外网服务端整理+文本教程+视频教程

任务完善 泉州三大BOSS 剑荡燕云 藏剑 通天顶 梁上等————–

css使用scoped之后样式失效问题

项目中的vue代码原本用的style标签来写css&#xff0c;现在想改成<style langscss scoped>&#xff0c;但是改完之后发现样式不对&#xff1a; 原来是&#xff1a; 将style改成scoped之后变成了&#xff1a;检查发现是之前定义的一些变量无法被识别&#xff0c;导致这些样…

大模型的开发应用(六):使用 Xtuner QLoRA 微调模型

这里写目录标题 0 前言1 Xtuner 简介1.1 主要特点1.2 核心功能1.3 优势与不足1.4 安装 2 数据集格式2.1 开源数据集2.2 自定义数据集2.3 数据集存放位置 3 微调大模型3.1 Qwen1.5的QLoRA配置文件3.2 修改配置文件&#xff08;1&#xff09;PART 1 基本设置&#xff08;2&#x…

cursor如何开启自动运行模式

在Cursor中&#xff0c;开启自动运行模式即启用“Yolo Mode”&#xff0c;具体操作如下&#xff1a; 按下Ctrl Shift J&#xff08;Windows/Linux&#xff09;或Cmd Shift J&#xff08;Mac&#xff09;打开Cursor设置。导航到“Features”&#xff08;功能&#xff09;选…