BFS入门刷题

article/2025/8/7 15:57:47

目录

P1746 离开中山路

P1443 马的遍历

P1747 好奇怪的游戏

P2385 [USACO07FEB] Bronze Lilypad Pond B


P1746 离开中山路

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int n;
int startx, starty;
int endx, endy;
char a[1010][1010];
int dis[1010][1010];
queue<pair<int, int>> q;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int bfs(int x, int y)
{memset(dis, -1, sizeof dis);dis[x][y] = 0;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 >= 1 && ny >= 1 && nx <= n && ny <= n && a[nx][ny] != '1' && dis[nx][ny] <= 0){q.push({nx, ny});dis[nx][ny] = dis[t.first][t.second] + 1;}}}return dis[endx][endy];
}
int main()
{cin >> n;for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){cin >> a[i][j];}}cin >> startx >> starty >> endx >> endy;cout << bfs(startx, starty);return 0;
}

P1443 马的遍历

2个样例TLE:

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};
int dy[] = {2, 1, -1, -2, -2, -1, 1, 2};
int dis[410][410];
int n, m;
queue<pair<int, int>> q;
int startx, starty;
int bfs(int x, int y)
{if (x == startx && y == starty){return 0;}memset(dis, -1, sizeof dis);q.push({startx, starty});dis[startx][starty] = 0;while (!q.empty()){auto t = q.front();q.pop();for (int i = 0; i < 8; 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] <= 0){q.push({nx, ny});dis[nx][ny] = dis[t.first][t.second] + 1;}}}return dis[x][y];
}
int main()
{cin >> n >> m >> startx >> starty;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){cout << bfs(i, j) << " ";}cout << endl;}return 0;
}

作者一开始想的是每个点都要计数,所以每个点都要搜一次,然后返回一个值

其实只要搜一次

用void类型,不用返回,从开始搜到结尾,然后每个点都会一层一层地搜到,最后dis数组里存的就是每个点的计数

优化:

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};
int dy[] = {2, 1, -1, -2, -2, -1, 1, 2};
int dis[410][410];
int n, m;
queue<pair<int, int>> q;
int startx, starty;
void bfs(int x, int y)
{memset(dis, -1, sizeof dis);q.push({startx, starty});dis[startx][starty] = 0;while (!q.empty()){auto t = q.front();q.pop();for (int i = 0; i < 8; 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] <= 0){q.push({nx, ny});dis[nx][ny] = dis[t.first][t.second] + 1;}}}
}
int main()
{cin >> n >> m >> startx >> starty;bfs(startx, starty);dis[startx][starty] = 0;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){cout << dis[i][j] << " ";}cout << endl;}return 0;
}

调用STL中的queue调用坐标需要花费比较长的时间,我们可以用数组来模拟queue节省时间

再优化:

#include <iostream>
#include <cstring>
using namespace std;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};
int dy[] = {2, 1, -1, -2, -2, -1, 1, 2};
int dis[410][410];
int n, m;
pair<int, int> q[410 * 410];
int startx, starty;
void bfs(int x, int y)
{memset(dis, -1, sizeof dis);int head = 0;int tail = 0;q[0] = {x, y};dis[x][y] = 0;while (head <= tail){auto t = q[head++];for (int i = 0; i < 8; 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] <= 0){q[++tail] = {nx, ny};dis[nx][ny] = dis[t.first][t.second] + 1;}}}
}
int main()
{cin >> n >> m >> startx >> starty;bfs(startx, starty);dis[startx][starty] = 0;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){cout << dis[i][j] << " ";}cout << endl;}return 0;
}

好像看不出什么区别

P1747 好奇怪的游戏

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int x1, y1;
int x2, y2;
int dis[30][30];
queue<pair<int, int>> q;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1, 2, 2, -2, -2};
int dy[] = {2, 1, -1, -2, -2, -1, 1, 2, 2, -2, -2, 2};
int bfs(int x, int y)
{memset(dis, -1, sizeof dis);q.push({x, y});dis[x][y] = 0;while (!q.empty()){auto t = q.front();q.pop();for (int i = 0; i < 12; i++){int nx = t.first + dx[i];int ny = t.second + dy[i];if (nx >= 1 && ny >= 1 && nx <= 20 && ny <= 20 && dis[nx][ny] <= 0){q.push({nx, ny});dis[nx][ny] = dis[t.first][t.second] + 1;}}}return dis[1][1];
}
int main()
{cin >> x1 >> y1 >> x2 >> y2;cout << bfs(x1, y1) << endl;cout << bfs(x2, y2) << endl;return 0;
}

P2385 [USACO07FEB] Bronze Lilypad Pond B

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
queue<pair<int, int>> q;
int m, n;
int m1, n1;
int a[35][35];
int dis[35][35];
int startx, starty, endx, endy;
int bfs(int x, int y)
{memset(dis, -1, sizeof dis);q.push({x, y});dis[x][y] = 0;while (!q.empty()){auto t = q.front();q.pop();int dx[] = {m1, m1, -m1, -m1, n1, n1, -n1, -n1};int dy[] = {n1, -n1, n1, -n1, m1, -m1, m1, -m1};for (int i = 0; i < 8; i++){int nx = t.first + dx[i];int ny = t.second + dy[i];if (nx >= 1 && ny >= 1 && nx <= m && ny <= n && a[nx][ny] != 0 && a[nx][ny] != 2 && dis[nx][ny] == -1){dis[nx][ny] = dis[t.first][t.second] + 1;q.push({nx, ny});}}}return dis[endx][endy];
}
int main()
{cin >> m >> n >> m1 >> n1;for (int i = 1; i <= m; i++){for (int j = 1; j <= n; j++){cin >> a[i][j];if (a[i][j] == 3){startx = i;starty = j;}if (a[i][j] == 4){endx = i;endy = j;}}}cout << bfs(startx, starty);return 0;
}

 


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

相关文章

Cypress + TypeScript + Vue3

🚀 从零构建 Cypress + TypeScript + Vue3 组件测试环境【详细实战教程】 组件测试是前端开发中不可忽视的一环,它能够帮助我们在开发阶段就发现 UI 与交互逻辑问题。本文将带你手把手搭建基于 Cypress + TypeScript + Vue3 的组件测试环境,包含完整目录结构、配置文件、组…

车辆检测算法在爆炸事故应急响应中的优化路径

视觉分析赋能车辆管控&#xff1a;以山东应急场景为例 背景&#xff1a;应急场景下的车辆管控痛点 近期山东多起爆炸事故暴露了应急响应中的车辆管理短板&#xff1a;消防车、救护车因违停车辆堵塞通道&#xff0c;违规车辆闯入事故核心区&#xff0c;传统监控系统依赖人工识别…

【小沐杂货铺】基于Three.JS绘制太阳系Solar System(GIS 、WebGL、vue、react,提供全部源代码)第2期

&#x1f37a;三维数字地球系列相关文章如下&#x1f37a;&#xff1a;1【小沐学GIS】基于C绘制三维数字地球Earth&#xff08;OpenGL、glfw、glut&#xff09;第一期2【小沐学GIS】基于C绘制三维数字地球Earth&#xff08;OpenGL、glfw、glut&#xff09;第二期3【小沐学GIS】…

π0论文阅读

https://www.physicalintelligence.company/download/pi0.pdf 模型输出的token&#xff0c;接diffusion模型&#xff0c;相比自OpenVLA那样的回归模型解码出action&#xff0c;输出更快&#xff0c;精度也会更高。 一、动作专家模块与流匹配&#xff08;Flow Matching&#xf…

安全漏洞修复导致SpringBoot2.7与Springfox不兼容

项目基于 springboot2.5.2 实现的&#xff0c;用 springfox-swagger2 生成与前端对接的 API 文档&#xff1b;pom.xml 中依赖如下 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId>&l…

VisionPro —— 不规则胶路检测

简介 本文介绍了一种基于Cognex视觉工具的胶路检测方法&#xff0c;分为直线和弧形两部分检测。 直线部分采用卡尺工具检测胶路宽度&#xff0c;通过动态调整仿射矩形区域进行多位置测量&#xff1b;弧形部分使用blob工具沿圆周设置检测区域。 两种方法均通过脚本实现工具映…

QT中更新或添加组件时出现“”qt操作至少需要一个处于启用状态的有效资料档案库“解决方法”

在MaintenanceTool.exe中点击下一步 第一个&#xff1a; 第二个&#xff1a; 第三个&#xff1a; 以上任意一个放入资料库中

【PyQt5】从零开始的PyQt5 - QLabel篇

从零开始的PyQt5 - QLabel篇 引言一、简述二、例程2.1 显示到QWidget窗口上2.2 重新设置Label大小和对齐方式2.3 添加内容&#xff0c;设置边框2.4 显示富文本 三、参考 引言 QLabel主要用于显示文本或图像&#xff0c;不提供用户交互功能。本文主要简述PyQt5中的QLabel以及展…

Microsoft Rewards——微软免费发钱!

Microsoft Rewards 是微软推出的用于推广其搜索引擎 Bing 的一项服务&#xff0c;用户只需要使用 Bing 进行任何搜索&#xff0c;就可以获得对应积分&#xff0c;可以兑换礼品卡等奖励。 ps&#xff1a;Bing本来就是中国大陆最好的搜索引擎。 举例&#xff1a;假设将 Bing 最为…

基于python的天气可视化系统的设计与实现

博主介绍&#xff1a;java高级开发&#xff0c;从事互联网行业六年&#xff0c;熟悉各种主流语言&#xff0c;精通java、python、php、爬虫、web开发&#xff0c;已经做了六年的毕业设计程序开发&#xff0c;开发过上千套毕业设计程序&#xff0c;没有什么华丽的语言&#xff0…

设计模式——组合设计模式(结构型)

摘要 组合设计模式是一种结构型设计模式&#xff0c;用于将对象组合成树形结构以表示“部分-整体”的层次结构&#xff0c;使客户端对单个对象和组合对象具有一致的访问方式。它包含抽象组件、叶子节点和组合节点&#xff0c;具有统一处理、支持递归结构和易扩展等优点&#x…

Launcher3体系化之路

&#x1f44b; 欢迎来到Launcher 3 背景 车企对于桌面的排版布局好像没有手机那般复杂&#xff0c;但也有一定的需求。部分场景下&#xff0c;要考虑的上下文比手机要多一些&#xff0c;比如有如下的一些场景&#xff1a; 手车互联。HiCar&#xff0c;CarPlay&#xff0c;An…

pikachu通关教程- over permission

如果使用A用户的权限去操作B用户的数据&#xff0c;A的权限小于B的权限&#xff0c;如果能够成功操作&#xff0c;则称之为越权操作。 越权漏洞形成的原因是后台使用了 不合理的权限校验规则导致的。 水平越权 当我们以Lucy账号登录&#xff0c;查询个人信息时&#xff0c;会有…

刷leetcode hot100--矩阵6/1

1.螺旋矩阵【很久】6/1【感觉就是思路的搬运工&#xff0c;没完全理解】 54. 螺旋矩阵 - 力扣&#xff08;LeetCode&#xff09; 原来想 但是如果是奇数矩阵&#xff0c;遍历不到中间 解决思路&#xff1a; 用left,right,top,down标记/限定每次遍历的元素&#xff0c;每次从…

Redis最佳实践——秒杀系统设计详解

基于Redis的高并发秒杀系统设计&#xff08;十万级QPS&#xff09; 一、秒杀系统核心挑战 瞬时流量洪峰&#xff1a;100万 QPS请求冲击库存超卖风险&#xff1a;精准扣减防止超卖系统高可用性&#xff1a;99.99%服务可用性要求数据强一致性&#xff1a;库存/订单/支付状态同步…

搭建基于VsCode的ESP32的开发环境教程

一、VsCode搜索ESP-IDF插件 根据插件处搜索找到ESP-IDF并安装 安装完成 二、配置安装ESP-IDF 配置IDF 按照如下配置&#xff0c;点击安装 安装完成 三、使用案例程序 创建一个闪光灯的例子程序&#xff0c;演示程序编译下载。 选择blink例子&#xff0c;闪烁LED的程序 选…

实现RabbitMQ多节点集群搭建

目录 引言 一、环境准备 二、利用虚拟机搭建 ​ 三、镜像集群配置 四、HAProxy实现负载均衡(主用虚拟机操作) 五、测试RabbitMQ集群搭建情况 引言 在现代分布式系统中&#xff0c;消息队列&#xff08;Message Queue&#xff09;扮演着至关重要的角色,而 RabbitMQ 作为…

B站视频下载器 v1.0.4|免登录下载1080P视频

核心亮点 ✅ 无需登录下载1080P高清视频✅ 支持Windows/macOS双平台✅ 纯净无广告完全免费✅ 可单独下载视频/音频/弹幕/字幕/封面 三步极简操作 粘贴B站视频链接选择保存位置点击「开始下载」 特色功能 独立下载选项&#xff08;视频/音频/弹幕/字幕/封面&#xff09;登录…

LLM-MPC混合架构:车载大语言模型用来增强自动驾驶系统

1. 概述 2025年&#xff0c;苏黎世研究团队在RSS2025会议上正式提出「LLM-MPC混合架构」&#xff0c;标志着大语言模型&#xff08;LLM&#xff09;在自动驾驶系统中的实用化迈出关键一步。该方案旨在解决传统深度学习模型在极端交通场景中泛化能力不足的问题。通过在车载终端…

leetcode-hot-100 (矩阵)

1、矩阵置零 题目链接&#xff1a;矩阵置零 题目描述&#xff1a;给定一个 m x n 的矩阵&#xff0c;如果一个元素为 0 &#xff0c;则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。 解答 方法一&#xff1a;使用一个二维数组 这是我看到这道题目的第一个想法&am…