模版总结小全

news2024/9/9 5:44:51

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.coloradmin.cn/o/1878027.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ardupilot开发 --- 坐标变换 篇

Good Morning, and in case I dont see you, good afternoon, good evening, and good night! 0. 一些概念1. 坐标系的旋转1.1 轴角法1.2 四元素1.3 基于欧拉角的旋转矩阵1.3.1 单轴旋转矩阵1.3.2 多轴旋转矩阵1.3.3 其他 2. 齐次变换矩阵3. visp实践 0. 一些概念 相关概念&am…

“论模型驱动架构设计方法及其应用”,软考高级论文,系统架构设计师论文

论文真题 模型驱动架构设计是一种用于应用系统开发的软件设计方法&#xff0c;以模型构造、模型转换和精化为核心&#xff0c;提供了一套软件设计的指导规范。在模型驱动架构环境下&#xff0c;通过创建出机器可读和高度抽象的模型实现对不同问题域的描述&#xff0c;这些模型…

自定义指令directive

一、在src目录下创建一个directive文件夹 test.ts文件存放创建的自定义指令&#xff0c;index.ts用于接收所有指令进行统一处理 二、编写自定义指令 // test.ts文件 export default {// 写个自定义指令mounted(el: any, binding: any) {console.log(el, binding, "&qu…

CC1利用链分析

分析版本 Commons Collections 3.1 JDK 8u65 环境配置参考JAVA安全初探(三):CC1链全分析 分析过程 我的Github主页Java反序列化学习同步更新&#xff0c;有简单的利用链图 首先看下CC1利用链的RCE利用点&#xff0c;在接口Transformer 接下来查看此接口的实现类&#xf…

将json对象转为xml进行操作属性

将json对象转为xml进行操作属性 文章目录 将json对象转为xml进行操作属性前端发送json数据格式写入数据库格式-content字段存储&#xff08;varchar(2000)&#xff09;Question实体类-接口映射对象QuestionContent 接收参数对象DAO持久层Mapper层Service层Controller控制层接收…

谈一下MySQL的两阶段提交机制

文章目录 为什么需要两阶段提交&#xff1f;两阶段提交流程&#xff1f;两阶段提交缺点&#xff1f; 为什么需要两阶段提交&#xff1f; 为了保证事务的持久性和一致性&#xff0c;MySQL需要确保redo log和binlog的同步持久化。MySQL通过“两阶段提交”的机制来实现在事务提交…

MyBatis第一节

目录 1. 简介2. 配置3. doing3.1 创建一个表3.2 打开IDEA&#xff0c;创建一个maven项目3.3 导入依赖的jar包3.4 创建entity3.5 编写mapper映射文件(编写SQL)3.6 编写主配置文件3.7 编写接口3.8 测试 参考链接 1. 简介 它是一款半自动的ORM持久层框架&#xff0c;具有较高的SQ…

【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…

【智能算法】决策树算法

目录 一、基本概念 二、工作原理 三、决策树算法优点和缺点 3.1 决策树算法优点 3.2 决策树算法缺点 四、常见的决策树算法及matlab代码实现 4.1 ID3 4.1.1 定义 4.1.2 matlab代码实现 4.2 C4.5 4.2.1 定义 4.2.2 matlab代码实现 4.3 CART 4.3.1 定义 4.3.2 mat…

leetcode-20-回溯-切割、子集

一、[131]分割回文串 给定一个字符串 s&#xff0c;将 s 分割成一些子串&#xff0c;使每个子串都是回文串。 返回 s 所有可能的分割方案。 示例: 输入: "aab" 输出: [ ["aa","b"], ["a","a","b"] ] 分析&…

springboot是否可以代替spring

Spring Boot不能直接代替Spring&#xff0c;但它是Spring框架的一个扩展和增强&#xff0c;提供了更加便捷和高效的开发体验。以下是关于Spring Boot和Spring关系的详细解释&#xff1a; Spring框架&#xff1a; Spring是一个广泛应用的开源Java框架&#xff0c;提供了一系列模…

Nosql期末复习

mongodb基本常用命令&#xff08;只要掌握所有实验内容就没问题&#xff09; 上机必考&#xff0c;笔试试卷可能考&#xff1a; 1.1 数据库的操作 1.1.1 选择和创建数据库 &#xff08;1&#xff09;use dbname 如果数据库不存在则自动创建&#xff0c;例如&#xff0c;以下…

ElementUI的基本搭建

目录 1&#xff0c;首先在控制终端中输入下面代码&#xff1a;npm i element-ui -S 安装element UI 2&#xff0c;构架登录页面&#xff0c;login.vue​编辑 3&#xff0c;在官网获取对应所需的代码直接复制粘贴到对应位置 4&#xff0c;在继续完善&#xff0c;从官网添加…

【工具分享】Nuclei

文章目录 NucleiLinux安装方式Kali安装Windows安装 Nuclei Nuclei 是一款注重于可配置性、可扩展性和易用性的基于模板的快速漏洞验证工具。它使用 Go 语言开发&#xff0c;具有强大的可配置性、可扩展性&#xff0c;并且易于使用。Nuclei 的核心是利用模板&#xff08;表示为简…

oracle 11g rac安装grid 执行root脚本add vip -n 。。。on node= ... failedFailed 错误处理

问题&#xff1a; CRS-4402: The CSS daemon was started in exclusive mode but found an active CSS daemon on node racdg1-1, number 1, and is terminating An active cluster was found during exclusive startup, restarting to join the cluster PRCN-2050 : The requ…

[OtterCTF 2018]Name Game

Name Game 题目描述&#xff1a;我们知道这个帐号登录到了一个名为Lunar-3的频道。账户名是什么&#xff1f;猜想&#xff1a;既然登陆了游戏&#xff0c;我们尝试直接搜索镜像中的字符串 Lunar-3 。 直接搜索 Lunar-3 先把字符串 重定向到 txt文件里面去然后里面查找 Lunar-3…

阐述Python:except的用法和作用?

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

C++特殊类设计单例模式...

文章目录 请设计一个类&#xff0c;不能被拷贝请设计一个类&#xff0c;只能在堆上创建对象请设计一个类&#xff0c;只能在栈上创建对象请设计一个类&#xff0c;不能被继承请设计一个类&#xff0c;只能创建一个对象(单例模式)单例模式&#xff1a;饿汉模式&#xff1a;懒汉模…

在vs上远程连接Linux写服务器项目并启动后,可以看到服务启动了,但是通过浏览器访问该服务提示找不到页面

应该是被防火墙挡住了&#xff0c;查看这个如何检查linux服务器被防火墙挡住 • Worktile社区 和这个关于Linux下Nginx服务启动&#xff0c;通过浏览器无法访问的问题_linux无法访问nginx-CSDN博客 的提示之后&#xff0c;知道防火墙开了&#xff0c;想着可能是我写的服务器的…

SpringDataJPA系列(1)JPA概述

SpringDataJPA系列(1)JPA概述 SpringDataJPA似乎越来越流行了&#xff0c;我厂的mysql数据库和MongoDB数据库持久层都依赖了SpringDataJPA。为了更好的使用它&#xff0c;我们内部还对MongoDB的做了进一步的抽象和封装。为了查漏补缺&#xff0c;温故而知新&#xff0c;整理下…