41 行代码C++算法模板
图的邻接表 BFS 搜索模板
BFS 搜索无权图中从起点到终点的最短路径长度(邻接表)
#include <bits/stdc++.h>
using namespace std;
const int N = 1005; // 根据题目点的数量范围调整
vector<int> adj[N]; // 邻接表(有权图需要使用结构体)
bool vis[N];#图#bfs#邻接表#最短路径
BFS 搜索无权图中从起点到终点的最短路径长度(邻接表)
#include <bits/stdc++.h>
using namespace std;
const int N = 1005; // 根据题目点的数量范围调整
vector<int> adj[N]; // 邻接表(有权图需要使用结构体)
bool vis[N];DFS 搜索无权图中从起点到终点的简单路径数量(邻接表)
#include <bits/stdc++.h>
using namespace std;
const int N = 1005; // 根据题目点的数量范围调整
vector<int> adj[N]; // 邻接表(有权图需要使用结构体)
bool vis[N];BFS 搜索二维矩阵中从 (1, 1) 到 (n, m) 的最短路径长度
#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
DFS 搜索二维矩阵中从 (1, 1) 到 (n, m) 的路线数量
#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
计算 a 的 b 次幂对 m 取模的结果,时间复杂度 O(log b)
using ll = long long;
ll quick_pow(ll a, ll b, ll m) {
ll ans = 1 % m;
ll w = a;
while (b) {
if (b % 2 == 1) ans = ans * w % m;二维数组枚举、BFS/DFS 常用的方向数组
// 上下左右四方向
int dx4[] = {-1, 1, 0, 0};
int dy4[] = {0, 0, -1, 1};
// 上下左右、左上右上、左下右下八方向
int dx8[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int dy8[] = {0, 0, -1, 1, -1, 1, -1, 1};