模版总结小全

BFS

最短步数问题

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

const int N = 50;
char g[N][N],d[N][N];
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
int n,m;

int bfs(int x,int y){
	queue<pair<int,int> > q;
	q.push({x,y});
	memset(d,-1,sizeof(d));
	d[x][y] = 1;
	while(!q.empty()){
		auto t = q.front();
		q.pop(); 
		for(int i = 0; i < 4; i++){
			int sx = t.first+dx[i];
			int sy = t.second+dy[i];
			if(sx >= 0 && sy >= 0 && sx < n && sy < m && d[sx][sy] == -1 && g[sx][sy] == '.'){
				q.push({sx,sy});
				d[sx][sy] = d[t.first][t.second]+1;
			}
		} 
	}
	return d[n-1][m-1];
}

int main(){
	cin >> n >> m;
	for(int i = 0; i < n; i++) cin >> g[i];
	cout << bfs(0,0) << endl;
	return 0;
} 

洪水填充:连通块问题

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;

const int N = 120;
char g[N][N];
bool d[N][N];
int n,m;
int dx[8] = {-1,-1,-1,0,1,1,1,0};
int dy[8] = {-1,0,1,1,1,0,-1,-1};

int bfs(int sx,int sy){
	queue<pair<int,int>> q;
	q.push({sx,sy});
	g[sx][sy] = '.';
	while(!q.empty()){
		auto h = q.front();
		q.pop();
		for(int i = 0; i < 8; i++){
			int x = h.first + dx[i];
			int y = h.second + dy[i];
			if(x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 'W'){
				q.push({x,y});
				g[x][y] = '.';
			} 
		}
	}
}

int main(){
	cin >> n >> m;
	int ans = 0;
	for(int i = 0; i < n; i++) cin >> g[i];
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(g[i][j] == 'W'){
				bfs(i,j);
				ans++;
			}
		}
	}
	cout << ans << endl;
	return 0;
}

最短路:输出路径

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;

const int N = 50;
int a[5][5];
bool st[N][N];

typedef pair<int,int> PII; 

PII h[25];
PII Prev[5][5];
int hh,tt;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

int bfs(int sx,int sy){
	hh = 0;
	tt = -1;
	h[++tt] = {sx,sy};
	memset(Prev,-1,sizeof Prev);
	while(hh <= tt){
		PII t = h[hh++];
		for(int i = 0; i < 4; i++){
			int x = t.first + dx[i];
			int y = t.second + dy[i];
			if(x >= 0 && x < 5 && y >= 0 && y < 5 && a[x][y] == 0 && st[x][y] == false){
				h[++tt] = {x,y};
				Prev[x][y] = t;
				st[x][y] = 1;
			}
		}
	}
} 


int main(){
	for(int i = 0; i < 5; i++){
		for(int j = 0; j < 5; j++){
			cin >> a[i][j];
		}
	}
	bfs(4,4);
	PII end(0,0);
	while(true){
		cout << "(" << end.first << ", " << end.second << ")" << endl;
		if(end.first == 4 && end.second == 4) break;
		end = Prev[end.first][end.second];
		
	}
}

DFS

组合型枚举:无限制数量

#include<iostream>
using namespace std;

int a[10000];
int n,sum; 

void dfs(int t,int idx,int s){
	if(s == n){
		cout << n << "=" << a[0];
		for(int i = 1; i < t; i++){
			cout << "+" << a[i];
		}
		cout << endl;
		sum++;
		return;
	}
	for(int i = idx; i < n; i++){
		if(i <= n-s+1){
			a[t] = i;
			dfs(t+1,i,s+i);
		} 
	}
}

int main(){
	cin >> n;
	dfs(0,1,0);	
	return 0;
} 

组合型枚举:有数量限制

#include<bits/stdc++.h>
using namespace std;
int a[1000];
int n,k;

void func(int m,int idx){
	if(m == k){
		for(int i = 0; i < m; i++) printf("%3d",a[i]);
		cout << endl;
	}
	for(int i = idx+1; i <= n; i++){ //idx为我上次找到元素的位置从后面继续找
		a[m] = i;
		func(m+1,i); //m+1搭配方案里的元素+1,idx=i为我现在看到的元素位置
	}
}

int main(){
	cin >> n >> k;
	func(0,0);
	return 0;
}

全排列

#include<iostream>
using namespace std;

int n;
int a[10000],v[1000000];

void dfs(int x){
	if(x == n){
		for(int i = 0; i < n; i++) cout << a[i] << " ";
		cout << endl;
	}
	for(int i = 1; i <= n; i++){
		if(v[i] == 0){
			a[x] = i;
			v[i] = 1;
			dfs(x+1);
			v[i] = 0;
		}
	}
}

int main(){
	cin >> n;
	dfs(0);
}

八皇后

#include<iostream>
using namespace std;

const int N = 1001;
char g[N][N];
bool col[N],dg[N],udg[N]; 
int n,cnt; 

void dfs(int u){
	if(u == n){
		cnt++;
		cout << "No. " << cnt << endl;
		for(int j = 0; j < n; j++){
			for(int i = 0; i < n; i++){
				if(g[j][i] == 'Q') cout << 1 << " ";
				else cout << 0 << " ";
			}
			cout << endl;
		} 
		return ;
	}
	for(int i = 0; i < n; i++){
		if(!col[i] && !dg[u+i] && !udg[n-u+i]){
			g[u][i] = 'Q';
			col[i] = dg[u+i] = udg[n-u+i] = true;
			dfs(u+1);
			col[i] = dg[u+i] = udg[n-u+i] = false;
			g[u][i] = '.';
		}
	}
} 

int main(){
	n = 8;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			g[i][j] = '.';
		}
	}
	dfs(0);
	return 0;
} 

图论:

        存储与遍历

               vectoer

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;

const int N = 1e3+10;
vector<vector<int> > g(N);
bool st[N];
int n,m; 

void bfs(int x){
	queue<int> q;
	q.push(x);
	st[x] = 1;
	cout << x << " ";
	while(!q.empty()){
		int t = q.front();
		q.pop();
		for(int i = 0; i < g[t].size(); i++){
			if(!st[g[t][i]]){
				q.push(g[t][i]);
				st[g[t][i]] = 1;
				cout << g[t][i] << " ";
			}
		}
	}
}

void dfs(int x){
	st[x] = 1;
	cout << x << " ";
	for(int i = 0; i < g[x].size(); i++){
		if(!st[g[x][i]]) dfs(g[x][i]);
	}
}

int main(){
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		int a,b; cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < g[i].size(); j++){
			cout << i << "---->" << g[i][j] << endl;
		}
	} 
	bfs(1);
	cout << endl;
	memset(st,0,sizeof(st));
	dfs(1);
	return 0;
} 


/*
9 8
1 2
1 7
1 4
2 8
2 5
4 3
3 9
4 6
*/ 

临接矩阵-数组

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;

const int N = 1e3+10;
int n,m;
int g[N][N];
int v[N];

void bfs(int x){
	queue<int> q;
	q.push(x);
	v[x] = 1;
	cout << x << " ";
	while(!q.empty()){
		int t = q.front();
		q.pop();
		for(int i = 1; i <= n; i++){
			if(g[t][i] && !v[i]){
				q.push(i);
				cout << i << " ";
				v[i] = 1;
			}
		}
	}
}

void dfs(int x){
	v[x] = 1;
	cout << x << " ";
	for(int i = 1; i <= n; i++){
		if(g[x][i] && !v[i]) dfs(i);
	}
}

int main(){
	int x; 
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		int a,b; cin >> a >> b;
		g[a][b] = g[b][a] = 1;
	}
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			if(g[i][j]) cout << i << "------>" << j << endl;
		}
	} 
	bfs(1); 
	cout << endl;
	memset(v,0,sizeof v);
	dfs(1);
	return 0;
} 


/*
9 8
1 2
1 7
1 4
2 8
2 5
4 3
3 9
4 6
*/ 

临接表-链表

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

const int N = 1e5 + 10, M = 1e6 + 10;
int st[N];
int n, m;
// h数组存储所有节点的边链表的头节点
// e数组存储的是节点 
int h[N], e[M], ne[M], w[M], idx;

void add(int a, int b) {
	e[idx] = b;
	ne[idx] = h[a];
	h[a] = idx;
	idx++;
}

void dfs(int u) {
	cout << u << " ";
	st[u] = 1;
	for(int i = h[u]; i != -1; i = ne[i]){
		int j = e[i];
		if(!st[j]) dfs(j);
	}
} 

void bfs(int u) {
	queue<int> q;
	q.push(u);
	st[u] = 1;
	cout << u << ' ';
	while(!q.empty()){
		int t = q.front();
		q.pop();
		for(int i = h[t]; i != -1; i = ne[i]){
			int j = e[i];
			if(!st[j]){
				cout << j << " ";
				st[j] = 1;
				q.push(j);
			} 
		} 
	} 
}


int main() {
	memset(h, -1, sizeof h);
	// 链式前向星
	 
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int a, b, c; cin >> a >> b;
		add(a, b);
		add(b, a);
	}
	
	for (int i = 1; i <= n; i++) { // 枚举每一个顶点 
		cout << i << "的所有边有这些:";  // 枚举当前顶点的所有边 
		for (int j = h[i]; j != -1; j = ne[j]) {
			cout << e[j] << " ";
		}
		cout << endl;
	}
	
	dfs(1);
	cout << endl;
	memset(st, 0, sizeof st);
	bfs(1);
	
    return 0;
}



/*
一个示例图: 无向图 
9 8
1 2
1 7
1 4
2 8
2 5
4 3
3 9
4 6
*/

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Kubernetes】搭建工具Kubeadm环境配置

架构&#xff1a;服务器采用Master-nodes&#xff08;3台&#xff09; Worker-nodes(2台) 一&#xff0c;服务准备工作 &#xff08;1&#xff09;在所有&#xff08;5台&#xff09;机器配置 主机名绑定&#xff0c;如下&#xff1a; cat /etc/hosts192.168.0.100 k8s-m…

短剧App开发的全攻略

短剧App开发的全攻略可以概括为以下几个关键步骤&#xff1a; 1、市场调研与需求分析 进行市场调研&#xff0c;研究目标用户群体&#xff0c;了解他们的需求和偏好。 观察竞争对手的App&#xff0c;分析他们的优点和缺点&#xff0c;以此为基础来制定自己的开发计划。 确定App…

【计算机网络】期末复习(2)

目录 第一章&#xff1a;概述 第二章&#xff1a;物理层 第三章&#xff1a;数据链路层 第四章&#xff1a;网络层 第五章&#xff1a;传输层 第一章&#xff1a;概述 三大类网络 &#xff08;1&#xff09;电信网络 &#xff08;2&#xff09;有线电视网络 &#xff0…

c++用什么软件编程?都有哪些?

c用什么软件编程&#xff1f;都有哪些&#xff1f; C 作为一种高效、面向对象的编程语言&#xff0c;广泛应用于软件开发、游戏开发、嵌入式系统等领域。那么在进行 C 编程时&#xff0c;我们通常会使用哪些软件呢&#xff1f;下面就来具体分析。 1. Visual Studio Visual Stu…

Autoware 学习

Autoware不同版本介绍 Autoware官方说明文档&#xff1a;https://autowarefoundation.github.io/autoware-documentation/main 使用ROS2和Autoware的自动驾驶汽车免费在线进阶课 译 https://bbs.huaweicloud.com/blogs/detail/283058 Autoware.AI 第一个基于 ROS 1 发布的 Au…

字节流和字符流的相关知识

目录 1. Writer1.1 写两行数据1.2 换一种方式1.3 追加数据1.4 写很多数据&#xff0c;记得要清一下缓存1.5 用数组、字符串写入 2. Reader2.1 读个文件2.2 读取字符2.3 读取数据到数组2.4 复制文件 3. InputStream4. OutputStream5. 参考链接 1. Writer Writer类是Java.io包中…

springcloud第4季 springcloud-alibaba之nacos+openfegin+gateway+sentinel熔断限流【经典案例】

一 说明 1.1 架构说明 本案例实现原理&#xff1a; 采用alibaba的nacos&#xff0c;openfegin&#xff0c;sentinel&#xff0c;gateway等组件实现熔断限流。 主要理解sentinel的ResouceSentinel和fallback的区别联系。 ResourceSentinel 主要是页面配置熔断限流规则&#…

python-20-零基础自学python-用类和while设计一个掷多次、多面骰子的工具的基础

学习内容&#xff1a;《python编程&#xff1a;从入门到实践》第二版 知识点&#xff1a;类、random、while循环、把while循环和类结合起来 练习内容&#xff1a; 练习9-13&#xff1a;骰子 创建一个Die类&#xff0c;它包含一个名为sides的属性&#xff0c;该属性的默认值…

open-chat-video-editor:开源短视频生成和编辑工具,以及抖音|TikTok 的移动端短视频项目

open-chat-video-editor&#xff1a;开源短视频生成和编辑工具&#xff0c;以及抖音|TikTok 的移动端短视频项目。 open-chat-video-editor&#xff1a;开源短视频生成和编辑工具 简介 Open Chat Video Editor是开源的短视频生成和编辑工具&#xff0c;整体技术框架如下&…

《昇思25天学习打卡营第11天 | 昇思MindSpore基于 MindSpore 实现 BERT 对话情绪识别》

11天本节学习到BERT全称是来自变换器的双向编码器表征量&#xff0c;它是Google于2018年末开发并发布的一种新型语言模型。BERT模型的主要创新点都在pre-train方法上&#xff0c;即用了Masked Language Model和Next Sentence Prediction两种方法分别捕捉词语和句子级别的repres…

Qt WPS(有源码)

项目源码地址&#xff1a;WPS完整源码 一.项目详情 该项目仿照WPS&#xff0c;实现了部分的功能&#xff0c;能够很方便对文本和HTML进行修改&#xff0c;并且有打印功能&#xff0c;可以很方便的生成PDF。 应用界面 项目架构分析 这个项目主要可分为两个部分&#xff0c;一…

长鑫存储母公司斥资24亿美元发展国产HBM

国产DRAM厂商长鑫存储母公司睿力集成计划投资24亿美元在上海建一座高端封装工厂。据报道&#xff0c;该工厂将专注于高带宽存储器&#xff08;HBM&#xff09;芯片的封装&#xff0c;预计到2026年中开始投入生产。长鑫存储将利用来自多方投资者的资金进行建设&#xff0c;其中包…

ElementUI框架搭建及组件使用

前言: 当开始使用ElementUI框架来搭建网站或Web应用程序时&#xff0c;了解框架的基本结构和组件的使用是至关重要的。ElementUI是一个基于Vue.js的框架&#xff0c;提供了丰富的UI组件和工具&#xff0c;可以帮助开发人员快速构建现代化的用户界面。 在本文中&#xff0c;我…

Go Error 处理

&#x1f49d;&#x1f49d;&#x1f49d;欢迎莅临我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:「stormsha的主页」…

如何正确使用C#短信接口发送招生短信

群发短信对教育机构来讲虽然是个不错的招生工具,但怎么使用决定着生源转化效率,如果是为了单纯的发短信而发短信效率当然不好,那么如何正确使用招生群发短信呢?技巧才是关键! 教育短信发送较多的就是招生群发短信内容,而运营商对教育行业内容审核一般比较严格,需要短信公司特殊…

springboot 3.x相比之前版本有什么区别

Spring Boot 3.x相比之前的版本&#xff08;尤其是Spring Boot 2.x&#xff09;&#xff0c;主要存在以下几个显著的区别和新特性&#xff1a; Java版本要求&#xff1a; Spring Boot 3.x要求至少使用Java 17作为最低版本&#xff0c;同时已经通过了Java 19的测试&#xff0c;…

如何焊铜管 量测射频前端模块

先说结论 要做Port Extension待测物要上电 且根据逻辑表给Enable pin上电网分输入功率 不要太大 -20dBm即可铜管的接地 要足够 以及足够近铜管与待测物之间 必要时 隔一颗电容不要将匹配元件 也包含在量测范围讯号针不要直接焊在焊盘上 首先 铜管要做Port…

Amazon Q——2023 re:Invent 大会的 AI 革新之星

引言 在2023年的 re:Invent 大会上&#xff0c;亚马逊云科技&#xff08;亚马逊云科技&#xff09;不仅展示了包括 Amazon Graviton3、Amazon SageMaker Studio Lab、Amazon Connect Wisdom、Amazon QuickSight Q 和 Amazon Private 5G 在内的多项创新产品&#xff0c;还发布了…

动手学深度学习(Pytorch版)代码实践 -计算机视觉-47转置卷积

47转置卷积 import torch from torch import nn from d2l import torch as d2l# 输入矩阵X和卷积核矩阵K实现基本的转置卷积运算 def trans_conv(X, K):h, w K.shapeY torch.zeros((X.shape[0] h - 1, X.shape[1] w - 1))for i in range(X.shape[0]):for j in range(X.shap…

MySQL高级-InnoDB引擎-事务日志- redo log(事务持久性的保证)

文章目录 1、redo log1.1、重做日志缓冲&#xff08;redo log buffer&#xff09;1.2、重做日志文件&#xff08;redo log file&#xff09; 2、如果没有redo log&#xff0c;可能会存在什么问题的&#xff1f;2.2、我们一起来分析一下。 2.2、那么&#xff0c;如何解决上述的问…