模版总结小全

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.mzph.cn/news/863331.shtml

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

相关文章

MySQL高级-SQL优化-insert优化-批量插入-手动提交事务-主键顺序插入

文章目录 1、批量插入1.1、大批量插入数据1.2、启动Linux中的mysql服务1.3、客户端连接到mysql数据库&#xff0c;加上参数 --local-infile1.4、查询当前会话中 local_infile 系统变量的值。1.5、开启从本地文件加载数据到服务器的功能1.6、创建表 tb_user 结构1.7、上传文件到…

mysql_config 命令, 可以查看mysqlclient库的位置在/usr/lib64/mysql下

好吧&#xff0c;其实我是从这里知道了 -l 后面加的库名和so文件这种名不一样&#xff0c;因为库文件实际叫下面这个名&#xff08;前面有lib)。

MySQL之覆盖索引

什么是覆盖索引&#xff1f; 覆盖索引&#xff1a;查询时使用了索引&#xff0c;且需要返回的列&#xff0c;在改索引中已经全部能找到。 示例&#xff1a;有user表如下&#xff1a; CREATE TABLE user (id bigint(20) NOT NULL AUTO_INCREMENT COMMENT 技术主键,name varch…

Git企业开发---初识Git

顾得泉&#xff1a;个人主页 个人专栏&#xff1a;《Linux操作系统》 《C从入门到精通》 《LeedCode刷题》 键盘敲烂&#xff0c;年薪百万&#xff01; 引言 不知道大家有没有经历这种困惑&#xff0c;当我们要去交某文档时&#xff0c;总是要进行修改&#xff0c;修改后再交…

Softmax函数的作用

Softmax 函数主要用于多类别分类问题&#xff0c;它将输入的数值转换为概率分布。 具体来说&#xff0c;对于给定的输入向量 x [x_1, x_2,..., x_n] &#xff0c;Softmax 函数的输出为 y [y_1, y_2,..., y_n] &#xff0c;其中&#xff1a; 这样&#xff0c;Softmax 函数的输…

人生最有力,最棒的十句话!

人生最有力&#xff0c;最棒的十句话 1、允许一切事发生&#xff0c;所有一切发生的事不是你能阻挡了的&#xff0c;你接受&#xff0c;他也发生&#xff0c;你不接受&#xff0c;他也发生&#xff0c;你还不如坦然面对接受现实。 2、你焦虑的时候千万不要躺着啥也不干&#xf…

全网唯一免费无水印AI视频工具!

最近Morph Studio开始免费公测&#xff01;支持高清画质&#xff0c;可以上传语音&#xff0c;同步口型&#xff0c;最重要的是生成的视频没有水印&#xff01; Morph Studio国内就可以访问&#xff0c;可以使用国内邮箱注册&#xff08;我用的163邮箱&#xff09;&#xff0c;…

Java--回顾方法的调用

1.静态方法与非静态方法 1.当二者皆为静态方式时&#xff0c;可直接类名.方法名调用其方法 2.当调用的方法是静态&#xff0c;被调用的方法为非静态时&#xff0c;调用将会报错 3.出现2情况可通过进行实例化这个类的方式进行调用&#xff0c;如图所示 4.当处于一个类下&#xf…

在IDEA中创建Maven项目

2023版IDEA创建Maven项目&#xff08;新版&#xff09; 1.打开IDEA&#xff0c;点击 文件 -> 新建 -> 项目 2.创建Maven项目 3.编写java文件并运行 在src -> java -> 创建一个java文件并运行 如果出现下图 解决办法&#xff1a; 2022版IDEA创建Maven项目&#xf…

判断时间序列中的元素是否为:年初、年末、季初、季末

【小白从小学Python、C、Java】 【考研初试复试毕业设计】 【Python基础AI数据分析】 判断时间序列中的元素是否为&#xff1a; 年初、年末、季初、季末 Series.dt.is_year_start Series.dt.is_year_end Series.dt.is_quarter_start Series.dt.is_quarter_end 选择题 关于以下…

J018_冒泡排序

一、排序过程 如果要对一个数组进行升序排序&#xff1a; 每个轮次两两数字进行比较&#xff0c;如果前面的数字大于后面的数字&#xff0c;则交换两个数字的位置&#xff1b;如果前面的数字小于或等于后面的数字&#xff0c;则这两个数字位置不变。直到把数组中所有数字比较…

Attention (注意力机制)

1. 背景&#xff1a; 字面的意思&#xff1a;给你一些东西(看见一个美女:).....)&#xff0c;你会注意什么&#xff1f; 大数据的时代下&#xff0c;有太多的数据&#xff0c;我们又该如何选择重要的数据呢&#xff1f; Attention 诞生了&#xff0c;但是又该如何去做呢(i.e., …

武汉星起航:亚马逊欧洲站潮流指南,满足年轻人选品需求

在充满活力的20-30岁年龄段&#xff0c;年轻人们充满朝气&#xff0c;追求时尚与品质&#xff0c;对生活充满无限期待。亚马逊欧洲站作为全球领先的电商平台&#xff0c;为这一年龄段的人群提供了丰富多样的商品选择。武汉星起航将为您介绍亚马逊欧洲站针对20-30岁人群的选品攻…

myCrayon个人博客项目基于springBoot+Vue全栈开发

目录 项目介绍 简介 项目架构 项目模块组成 数据库设计 项目展示 首页 用户登录与注册 个人信息模块 商城展示 博客模块 博客浏览 博客发布与编辑 博客搜索 社区模块 新闻模块 后台管理系统 部署方式 结语 项目介绍 简介 项目类似于CSDN&#xff0c;支持所…

2024年4家HTTP代理服务商网站最新测评

一、芝麻HTTP芝麻HTTP作为代理服务领域的佼佼者&#xff0c;其HTTP代理服务同样表现出色。凭借海量IP资源和高效稳定的性能&#xff0c;芝麻HTTP为用户提供了卓越的代理服务体验。 特点与优势 ① 海量IP资源&#xff1a;拥有庞大的代理IP池&#xff0c;确保用户能够随时获取到…

Django 自定义标签

1&#xff0c;简单标签 1.1 添加自定义标签函数 Test/app5/templatetags/mytags.py from django import template register template.Library() register.simple_tag() def show_title(value, n):if len(value) > n:return f{value[:n]}...else:return value 1.2 添加视…

Typora failed to export as pdf. undefined

变换版本并没有用&#xff0c;调整图片大小没有用 我看到一个博客后尝试出方案 我的方法 解决&#xff1a;从上图中的A4&#xff0c;变为其他&#xff0c;然后变回A4 然后到处成功&#xff0c;Amazing&#xff01; 参考&#xff1a; Typora 导出PDF 报错 failed to export…

javaScript利用indexOf()查找字符串的某个字符出现的位置

1 创建字符串 2 利用indexof()查询字符串的字符 3 利用while循环判断indexOf是否等于-1&#xff0c;不等于-1就打印一次并且索引号1去查下一个字符 //创建字符串var str1234567812311231;var indexstr.indexOf(1);//查询该字符while(index !-1)//indexOf()没有查到会返回-1{…

【Linux】使用chrony同步时间

chrony介绍 chrony 是一个开源的网络时间协议 (NTP) 客户端和服务器&#xff0c;旨在保持计算机系统的时间精确同步。它是Linux和其他类Unix系统中广泛使用的工具&#xff0c;特别是在需要高精度时间同步的环境中。chrony 的设计考虑了现代网络的挑战&#xff0c;如不稳定的连…

负载均衡器有什么用?

负载均衡器有什么用&#xff1f; 负载均衡器是一种在多个服务器之间分配网络或应用程序流量的设备或软件应用程序。其主要目的是确保没有一台服务器承担过多的需求&#xff0c;从而提高应用程序的响应速度和可用性。 在计算机发展的早期&#xff0c;负载均衡是一个手动过程。…