广度优先搜索(BFS)

AcWing 844. 走迷宫

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;const int N = 110;
typedef pair<int, int>  PII;int n, m;
int g[N][N], d[N][N];int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};void bfs() {queue<PII> q;q.push({0, 0});d[0][0] = 0;while(q.size()) {auto t = q.front(); q.pop();int x = t.first, y = t.second;for (int i = 0; i < 4; i ++ ) {int a = x + dx[i], b = y + dy[i];if (a >= 0 && a < n && b >= 0 && b < m && !d[a][b] && !g[a][b]) {d[a][b] = d[x][y] + 1;q.push({a, b});}} } 
}int main() {cin >> n >> m;for (int i = 0; i < n; i ++ ) {for (int j = 0; j < m; j ++ ) {cin >> g[i][j];}}bfs();cout << d[n-1][m-1];return 0;
}

AcWing 845. 八数码

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;string s;
string t;
unordered_map<string, int> d;int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};int bfs(string s) {queue<string> q;string end = "12345678x";q.push(s);while(q.size()) {auto t = q.front(); q.pop();if (t == end) return d[t];int dist = d[t];int k = t.find("x");int x = k / 3, y = k % 3;for (int i = 0; i < 4; i ++ ) {int a = x + dx[i], b = y + dy[i];if (a >= 0 && a < 3 && b >=0 && b < 3) {swap(t[a *3 + b], t[k]);if (!d.count(t)) {d[t] = dist + 1;q.push(t);}swap(t[a * 3 + b], t[k]);}}}return -1;
}int main() {for (int i = 1; i <= 9; i ++ ) {cin >> t;s += t;}cout <<  bfs(s);return 0;
}

AcWing 179. 八数码

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;string s, str;
unordered_map<string, int> d;int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};char op[] = "udlr";unordered_map<string, pair<string, char> >pre;int bfs() {queue<string> q;q.push(str);d[str] = 0;string end = "12345678x";while(q.size()) {auto t = q.front(); q.pop();if(t == end) return d[t];string s = t;int k = t.find('x');int x = k/3, y = k%3;for(int i=0; i<4; i++) {int a = x + dx[i], b = y + dy[i];if(a>=0 && a<3 && b>=0 && b<3) {swap(t[a*3+b], t[k]);if(!d.count(t)) {pre[t] = {s, op[i]};d[t] = d[s] + 1;q.push(t);}swap(t[a*3+b], t[k]);}}}return -1;
}int main() {for(int i=0; i<9; i++) {cin >> s;str += s;}int t = bfs();if(t!=-1) {string res;string end = "12345678x";while(1) {string t = pre[end].first;res += pre[end].second;if(t == str) break;end = t;}reverse(res.begin(), res.end());cout << res;} else {cout << "unsolvable";}// cout << t;return 0;
}

AcWing 847. 图中点的层次

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;const int N = 1e5 + 10;vector<int> G[N];int n, m;int d[N];int bfs() {memset (d, -1, sizeof d);queue<int> q;q.push(1);d[1] = 0;while(q.size()) {auto t = q.front(); q.pop();for (auto j : G[t]) {if (d[j] == -1) {d[j] = d[t] + 1;q.push(j);}}}return d[n];
}int main() {cin >> n >> m;for (int i = 0; i < m; i ++ ) {int a, b;cin >> a >> b;G[a].push_back(b);}cout << bfs();return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/443248.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【学习笔记】Vue3源码解析:第一部分-实现vue3环境搭建

课程地址&#xff1a;【已完结】全网最详细Vue3源码解析&#xff01;&#xff08;一行行带你手写Vue3源码&#xff09; 第一部分&#xff1a;实现vue3环境搭建&#xff08;对应课程的第1-3节&#xff09; VUE2与VUE3的对比&#xff1a; 也即vue2的痛点&#xff1a; 对TypeSc…

【Algorithms 4】算法(第4版)学习笔记 02 - 1.4 算法分析

文章目录 前言参考目录学习笔记1&#xff1a;科学方法2&#xff1a;观察举例&#xff1a;三数之和3&#xff1a;近似4&#xff1a;增长数量级4.1&#xff1a;二分查找 demo4.2&#xff1a;二分查找代码实现4.3&#xff1a;二分查找比较次数的证明&#xff08;比较次数最多为lgN…

知识推理的多重途径

目录 前言1 逻辑及推理简介2 演绎推理&#xff1a;Top-Down Logic2.1 肯定前件假言推理2.2 否定后件假言推理2.3 演绎推理的逻辑链条 3 归纳推理&#xff1a;Bottom-Up Logic3.1 从特例到一般3.2 逐步推导的过程 4 溯因推理&#xff1a;结果的可解释逻辑4.1 推断过程的回溯4.2 …

西瓜书读书笔记整理(十二) —— 第十二章 计算学习理论(下)

第十二章 计算学习理论&#xff08;下&#xff09; 12.4 VC 维&#xff08;Vapnik-Chervonenkis dimension&#xff09;12.4.1 什么是 VC 维12.4.2 增长函数&#xff08;growth function&#xff09;、对分&#xff08;dichotomy&#xff09;和打散&#xff08;shattering&…

[Java 并发基础]多线程编程

文章参考&#xff1a; https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html https://juejin.cn/post/6970558076642394142 文章目录 线程的创建方式继承 Thread实现 Runnable 接口实现 Callable 接口使用 Lambda使用线程池 线程创建相关的 jdk源码Thr…

“死“社群先不要扔,想办法激活一下,隔壁的运营都馋哭了

私域运营已成为当下很多企业寻求增长的标配。在这过程中&#xff0c;社群运营就是极为重要的一个环节。过去我们为了流量&#xff0c;疯狂建群拉人。但建社群容易活跃难&#xff0c;活跃一段时间后&#xff0c;社群会越来越安静。 不仅如此&#xff0c;群主和管理员也渐渐疏于…

优化器刺客之limit 1--Order by col limit n 代价预估优化探索

一、现象 order by 排序加了limit后更慢了&#xff1f; test# explain analyze select userid from dba_users where username like %aaaaaaaaaaaaaaaaaa% order by userid ;QUERY PLAN --------------…

CentOS7中安装ElasticSearch

文章目录 检测是否安装了Elasticsearch安装JDK下载java配置 下载Elasticsearch解压安装Elasticsearch修改配置文件启动Elasticsearch常见问题 ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎&#xff0c;基于RESTful web接口。Elasti…

【数据结构 02】队列

一、原理 队列通常是链表结构&#xff0c;只允许在一端进行数据插入&#xff0c;在另一端进行数据删除。 队列的特性是链式存储&#xff08;随机增删&#xff09;和先进先出&#xff08;FIFO&#xff1a;First In First Out&#xff09;。 队列的缺陷&#xff1a; 不支持随机…

【C/C++】C/C++编程——整型(一)

整型 C 中的整型是基本的数据类型之一&#xff0c;用于表示没有小数部分的数。这包括正整数、负整数以及零。C 提供了多种整型&#xff0c;以适应不同大小的数值需求和优化内存使用。 整型的种类 C 中的整型可以根据其大小&#xff08;即占用的字节数&#xff09;和能够表示…

方法阻塞的解决方案之一

1、简单使用 一个h一个cpp文件 #pragma once #include <iostream> #include <thread> #include <atomic> #include <chrono> #include <string>class Person {public:struct dog {std::string name;int age;};public:void a(std::atomic<bo…

笔记本从零安装ubuntu server系统+环境配置

文章目录 前言相关链接ubuntu Server 安装教程屏幕自动息屏关上盖子不休眠MobaXterm外网SSH内网穿透IPV6远程 为什么我要笔记本装Linux为什么要换ubuntu Server版能否连接wifi之后Linux 配置清单总结 前言 之前装了个ubuntu desktop 版&#xff0c;发现没有命令行&#xff0c;…