This commit is contained in:
HuangHai
2025-08-30 18:35:01 +08:00
commit 1f397eca87
6258 changed files with 1487241 additions and 0 deletions

213
TangDou/Dfs/.clang-format Normal file
View File

@@ -0,0 +1,213 @@
# 语言: None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto
Language: Cpp
BasedOnStyle: LLVM
# 访问说明符(public、private等)的偏移
AccessModifierOffset: -4
# 开括号(开圆括号、开尖括号、开方括号)后的对齐: Align, DontAlign, AlwaysBreak(总是在开括号后换行)
AlignAfterOpenBracket: Align
# 连续赋值时,对齐所有等号
AlignConsecutiveAssignments: false
# 连续声明时,对齐所有声明的变量名
AlignConsecutiveDeclarations: false
# 右对齐逃脱换行(使用反斜杠换行)的反斜杠
AlignEscapedNewlines: Right
# 水平对齐二元和三元表达式的操作数
AlignOperands: true
# 对齐连续的尾随的注释
AlignTrailingComments: true
# 不允许函数声明的所有参数在放在下一行
AllowAllParametersOfDeclarationOnNextLine: false
# 不允许短的块放在同一行
AllowShortBlocksOnASingleLine: true
# 允许短的case标签放在同一行
AllowShortCaseLabelsOnASingleLine: true
# 允许短的函数放在同一行: None, InlineOnly(定义在类中), Empty(空函数), Inline(定义在类中,空函数), All
AllowShortFunctionsOnASingleLine: None
# 允许短的if语句保持在同一行
AllowShortIfStatementsOnASingleLine: true
# 允许短的循环保持在同一行
AllowShortLoopsOnASingleLine: true
# 总是在返回类型后换行: None, All, TopLevel(顶级函数,不包括在类中的函数),
# AllDefinitions(所有的定义,不包括声明), TopLevelDefinitions(所有的顶级函数的定义)
AlwaysBreakAfterReturnType: None
# 总是在多行string字面量前换行
AlwaysBreakBeforeMultilineStrings: false
# 总是在template声明后换行
AlwaysBreakTemplateDeclarations: true
# false表示函数实参要么都在同一行,要么都各自一行
BinPackArguments: true
# false表示所有形参要么都在同一行,要么都各自一行
BinPackParameters: true
# 大括号换行,只有当BreakBeforeBraces设置为Custom时才有效
BraceWrapping:
# class定义后面
AfterClass: false
# 控制语句后面
AfterControlStatement: false
# enum定义后面
AfterEnum: false
# 函数定义后面
AfterFunction: false
# 命名空间定义后面
AfterNamespace: false
# struct定义后面
AfterStruct: false
# union定义后面
AfterUnion: false
# extern之后
AfterExternBlock: false
# catch之前
BeforeCatch: false
# else之前
BeforeElse: false
# 缩进大括号
IndentBraces: false
# 分离空函数
SplitEmptyFunction: false
# 分离空语句
SplitEmptyRecord: false
# 分离空命名空间
SplitEmptyNamespace: false
# 在二元运算符前换行: None(在操作符后换行), NonAssignment(在非赋值的操作符前换行), All(在操作符前换行)
BreakBeforeBinaryOperators: NonAssignment
# 在大括号前换行: Attach(始终将大括号附加到周围的上下文), Linux(除函数、命名空间和类定义,与Attach类似),
# Mozilla(除枚举、函数、记录定义,与Attach类似), Stroustrup(除函数定义、catch、else,与Attach类似),
# Allman(总是在大括号前换行), GNU(总是在大括号前换行,并对于控制语句的大括号增加额外的缩进), WebKit(在函数前换行), Custom
# 注:这里认为语句块也属于函数
BreakBeforeBraces: Custom
# 在三元运算符前换行
BreakBeforeTernaryOperators: false
# 在构造函数的初始化列表的冒号后换行
BreakConstructorInitializers: AfterColon
#BreakInheritanceList: AfterColon
BreakStringLiterals: false
# 每行字符的限制,0表示没有限制
ColumnLimit: 0
CompactNamespaces: true
# 构造函数的初始化列表要么都在同一行,要么都各自一行
ConstructorInitializerAllOnOneLineOrOnePerLine: false
# 构造函数的初始化列表的缩进宽度
ConstructorInitializerIndentWidth: 4
# 延续的行的缩进宽度
ContinuationIndentWidth: 4
# 去除C++11的列表初始化的大括号{后和}前的空格
Cpp11BracedListStyle: true
# 继承最常用的指针和引用的对齐方式
DerivePointerAlignment: false
# 固定命名空间注释
FixNamespaceComments: true
# 缩进case标签
IndentCaseLabels: false
IndentPPDirectives: None
# 缩进宽度
IndentWidth: 4
# 函数返回类型换行时,缩进函数声明或函数定义的函数名
IndentWrappedFunctionNames: false
# 保留在块开始处的空行
KeepEmptyLinesAtTheStartOfBlocks: false
# 连续空行的最大数量
MaxEmptyLinesToKeep: 1
# 命名空间的缩进: None, Inner(缩进嵌套的命名空间中的内容), All
NamespaceIndentation: None
# 指针和引用的对齐: Left, Right, Middle
PointerAlignment: Right
# 允许重新排版注释
ReflowComments: true
# 允许排序#include
SortIncludes: false
# 允许排序 using 声明
SortUsingDeclarations: false
# 在C风格类型转换后添加空格
SpaceAfterCStyleCast: false
# 在Template 关键字后面添加空格
SpaceAfterTemplateKeyword: true
# 在赋值运算符之前添加空格
SpaceBeforeAssignmentOperators: true
# SpaceBeforeCpp11BracedList: true
# SpaceBeforeCtorInitializerColon: true
# SpaceBeforeInheritanceColon: true
# 开圆括号之前添加一个空格: Never, ControlStatements, Always
SpaceBeforeParens: ControlStatements
# SpaceBeforeRangeBasedForLoopColon: true
# 在空的圆括号中添加空格
SpaceInEmptyParentheses: false
# 在尾随的评论前添加的空格数(只适用于//)
SpacesBeforeTrailingComments: 1
# 在尖括号的<后和>前添加空格
SpacesInAngles: false
# 在C风格类型转换的括号中添加空格
SpacesInCStyleCastParentheses: false
# 在容器(ObjC和JavaScript的数组和字典等)字面量中添加空格
SpacesInContainerLiterals: true
# 在圆括号的(后和)前添加空格
SpacesInParentheses: false
# 在方括号的[后和]前添加空格,lamda表达式和未指明大小的数组的声明不受影响
SpacesInSquareBrackets: false
# 标准: Cpp03, Cpp11, Auto
Standard: Cpp11
# tab宽度
TabWidth: 4
# 使用tab字符: Never, ForIndentation, ForContinuationAndIndentation, Always
UseTab: Never

View File

@@ -0,0 +1,25 @@
{
"configurations": [
{
"name": "Win32",
"compilerArgs": [
"-fexec-charset=GBK"
],
"includePath": [
"${workspaceFolder}/**",
"C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/**",
"C:/TDM-GCC-64/x86_64-w64-mingw32/include/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/TDM-GCC-64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}

5
TangDou/Dfs/.vscode/extensions.json vendored Normal file
View File

@@ -0,0 +1,5 @@
{
"recommendations": [
"MS-CEINTL.vscode-language-pack-zh-hans"
]
}

28
TangDou/Dfs/.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,28 @@
// https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,cppdbg对应cpptools提供的调试功能;可以认为此处只能是cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,相当于在main上打断点
"externalConsole": false,
"cwd": "${fileDirname}\\", // 调试程序时的工作目录,此为工作区文件夹;改成${fileDirname}可变为文件所在目录
"environment": [], // 环境变量
"internalConsoleOptions": "neverOpen", // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡,你应该不需要对gdb手动输命令吧?
"MIMode": "gdb", // 指定连接的调试器,可以为gdb或lldb。但我没试过lldb
"miDebuggerPath": "gdb.exe", // 调试器路径,Windows下后缀不能省略,Linux下则不要
"setupCommands": [
{ // 模板自带,好像可以更好地显示STL容器的内容,具体作用自行Google
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "Compile" // 调试会话开始前执行的任务,一般为编译程序。与tasks.json的label相对应
}
]
}

112
TangDou/Dfs/.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,112 @@
{
"files.defaultLanguage": "c", // ctrl+N新建文件后默认的语言
"editor.formatOnType": true, // 输入分号(C/C++的语句结束标识)后自动格式化当前这一行的代码
"editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳转点,不用这个就必须手动触发Intellisense了
"editor.acceptSuggestionOnEnter": "off", // 我个人的习惯,按回车时一定是真正的换行,只有tab才会接受Intellisense
"code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入
"code-runner.saveFileBeforeRun": true, // run code前保存
"code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
"code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息,默认false
"code-runner.ignoreSelection": true, // 默认为false,效果是鼠标选中一块代码后可以单独执行,但C是编译型语言,不适合这样用
"C_Cpp.clang_format_sortIncludes": true,
"files.associations": {
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"optional": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"cassert": "cpp",
"cerrno": "cpp",
"cfloat": "cpp",
"chrono": "cpp",
"ciso646": "cpp",
"climits": "cpp",
"complex": "cpp",
"csetjmp": "cpp",
"csignal": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cuchar": "cpp",
"numeric": "cpp",
"ratio": "cpp",
"version": "cpp",
"bitset": "cpp",
"regex": "cpp",
"random": "cpp",
"unordered_set": "cpp",
"list": "cpp",
"any": "cpp",
"functional": "cpp",
"mutex": "cpp",
"shared_mutex": "cpp",
"thread": "cpp",
"variant": "cpp",
"map": "cpp",
"scoped_allocator": "cpp",
"future": "cpp",
"iomanip": "cpp",
"forward_list": "cpp",
"bit": "cpp",
"ccomplex": "cpp",
"cfenv": "cpp",
"charconv": "cpp",
"cinttypes": "cpp",
"codecvt": "cpp",
"condition_variable": "cpp",
"cstdalign": "cpp",
"cstdbool": "cpp",
"ctgmath": "cpp",
"set": "cpp",
"iterator": "cpp",
"filesystem": "cpp",
"ios": "cpp",
"locale": "cpp",
"queue": "cpp",
"stack": "cpp",
"typeindex": "cpp",
"compare": "cpp",
"concepts": "cpp",
"coroutine": "cpp",
"numbers": "cpp",
"ranges": "cpp",
"span": "cpp",
"stop_token": "cpp"
}, // 格式化时调整include的顺序(按字母排序)
}

40
TangDou/Dfs/.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,40 @@
// https://code.visualstudio.com/docs/editor/tasks
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile", // 任务名称,与launch.json的preLaunchTask相对应
"command": "g++", // 要使用的编译器,C++用g++
"args": [
"${file}",
"-o", // 指定输出文件名,不加该参数则默认输出a.exe,Linux下默认a.out
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-g", // 生成和调试有关的信息
"-Wall", // 开启额外警告
"-static-libgcc", // 静态链接libgcc,一般都会加上
"-fexec-charset=GBK", // 生成的程序使用GBK编码,不加这一条会导致Win下输出中文乱码
"-std=c++17", // C++最新标准为c++17,或根据自己的需要进行修改
], // 编译的命令,其实相当于VSC帮你在终端中输了这些东西
"type": "process", // process是vsc把预定义变量和转义解析后直接全部传给command;shell相当于先打开shell再输入命令,所以args还会经过shell再解析一遍
"group": {
"kind": "build",
"isDefault": true // 不为true时ctrl shift B就要手动选择了
},
"presentation": {
"echo": true,
"reveal": "always", // 执行任务时是否跳转到终端面板,可以为always,silent,never。具体参见VSC的文档
"focus": false, // 设为true后可以使执行task时焦点聚集在终端,但对编译C/C++来说,设为true没有意义
"panel": "shared" // 不同的文件的编译信息共享一个终端面板
},
},
{
"label": "commit gitee",
"type": "shell",
"command": "D:\\python\\一键推拉.cmd",
"group": {
"kind": "build",
"isDefault": true
},
}
]
}

View File

@@ -0,0 +1,44 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
vector<int> a;
int st[N];
int n, r;
/**
测试用例:
3 2
1 2
1 3
2 1
2 3
3 1
3 2
*/
void dfs(int step, int m) {
if (m == r) {
for (int i = 0; i < a.size(); i++) printf("%d ", a[i]);
printf("\n");
return;
}
if (step == n + 1)return;
//每个数字都可尝试放进当前箱子
for (int i = 1; i <= n; i++) {
//如果没有用过
if (!st[i]) {
//选择
a.push_back(i);
st[i] = true;
dfs(step + 1, m + 1);
st[i] = false;
a.pop_back();
}
}
}
int main() {
cin >> n >> r;
dfs(1, 0);
return 0;
}

View File

@@ -0,0 +1,33 @@
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
int m;
int cnt;
/**
* 功能:整数的拆分
* @param n 要拆分的数
*/
void dfs(int n) {
if (n == 0 && a.size() > 1) {
for (int i = 0; i < a.size(); i++) printf("%d ", a[i]);
printf("\n");
cnt++;
return;
}
// a 2 3 4 ->5 6 7
for (int i = a.size() == 0 ? 1 : a[a.size() - 1]; i <= n; i++) {
a.push_back(i);
dfs(n - i);
a.pop_back();
}
}
int main() {
cin >> m;
dfs(m);
//总数量
printf("%d ", cnt);
return 0;
}

View File

@@ -0,0 +1,81 @@
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
#define x first
#define y second
typedef pair<int, int> PII;
int a[N][N];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int n, m, Max;
vector<PII> s;
/**
测试数据
4 4
BLQB
BBQS
SBQL
QQQQ
输出
2
3 3
LSB
QBQ
BSL
输出
-1
*/
/**
* 功能:深度优先搜索
* @param x 横坐标
* @param y 纵坐标
* @param step 步数
*/
void dfs(int x, int y, int step) {
//如果死循环,一直
if (step > n * m) {
cout << -1 << endl;
exit(0);
}
//更新步数
Max = max(Max, step);
//4个方向
for (int i = 0; i <= 3; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
//如果出界
if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
//下一步与期望值对比
if (a[tx][ty] == a[x][y] + 1 || (a[tx][ty] == 1 && a[x][y] == 4))
dfs(tx, ty, step + 1);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
char x;
cin >> x;
//记录起点
if (x == 'L')
a[i][j] = 1, s.push_back({i, j});
else if (x == 'Q') a[i][j] = 2;
else if (x == 'B') a[i][j] = 3;
else if (x == 'S') a[i][j] = 4;
}
//模拟从每个起点开始
for (int i = 0; i < s.size(); i++)
dfs(s[i].x, s[i].y, 1);
//长度除以4
cout << Max / 4 << endl;
return 0;
}

38
TangDou/Dfs/DFS13.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m, Max;
int b1[N]; //正对角线
int b2[N]; //反对角线
//就是加一个常数增量,将x-y与y-x转化为正数,当然大于10的数字也可以,但对应的数组长度要装的下才行。
const int M = 30;
void dfs(int x, int y, int sum) {
if (x == n + 1) {
//收集最大值
Max = max(Max, sum);
return;
}
//如果这个位置可以放,那么我们可以选择放
if (!b1[x + y] && !b2[x - y + M]) {
b1[x + y] = 1, b2[x - y + M] = 1;
if (y == m)
dfs(x + 1, 1, sum + 1);
else
dfs(x, y + 1, sum + 1);
b1[x + y] = 0, b2[x - y + M] = 0;
}
//不管可以不可以放,都可以选择不放
if (y == m)dfs(x + 1, 1, sum);
else dfs(x, y + 1, sum);
}
int main() {
cin >> n >> m; // n行m列
dfs(1, 1, 0); //从(1,1)位置出发,0:表示已经安排好的传教士数量
cout << Max << endl;
return 0;
}

33
TangDou/Dfs/DFS14.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 25;
int a[N][N]; // 记录用户输入的数据,工作i由j这个人来完成需要的时间
bool st[N]; // 是不是已经分配完
int n, ans = INF;
void dfs(int step, int sum) {
if (step == n + 1) {
ans = min(ans, sum);
return;
}
for (int i = 1; i <= n; i++) //枚举人员
if (!st[i]) {
st[i] = true;
dfs(step + 1, sum + a[step][i]);
st[i] = false;
}
}
int main() {
cin >> n; // n个人,n个任务
// a[i][j]指的是第i项工作如果由j号员工完成所需要的时间
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> a[i][j];
dfs(1, 0);
cout << ans << endl;
return 0;
}

View File

@@ -0,0 +1,32 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N], n;
int b1[N];//列桶
int b2[N];//正对角线桶
int b3[N];//反对角线桶
int cnt;
void dfs(int x) {
if (x == n + 1) {
cnt++;
for (int i = 1; i <= n; i++) cout << a[i];
cout << endl;
return;
}
for (int y = 1; y <= n; y++) {
if (!b1[y] && !b2[x + y] && !b3[x - y + 8]) {
a[x] = y;
b1[y] = b2[x + y] = b3[x - y + 8] = 1;
dfs(x + 1);
b1[y] = b2[x + y] = b3[x - y + 8] = 0;
}
}
}
int main() {
cin >> n;
dfs(1);
cout << cnt << endl;
return 0;
}

View File

@@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int a[N];
bool st[N];
int n = 3;
//函数说明:填充第几个盒子
void dfs(int step) {
//如果全部填充完毕,走到了最后虚拟的n+1号盒子面前,就表示前面都完成了正确填充
if (step == n + 1) {
for (int i = 1; i <= n; i++) cout << a[i] << " ";
cout << endl;
return;
}
//每一个数字都可以用来填充
for (int i = 1; i <= n; i++)
//如果没有使用过
if (!st[i]) {
//放里
a[step] = i;
//标识已使用
st[i] = true;
//填充下一个
dfs(step + 1);
//标识未使用
st[i] = false;
}
}
int main() {
//开始填充第一个盒子
dfs(1);
return 0;
}

View File

@@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int a[N];
bool st[N];
int n = 9;
void dfs(int step) {
if (step == n + 1) {
if ((a[1] * 100 + a[2] * 10 + a[3]) + (a[4] * 100 + a[5] * 10 + a[6]) == a[7] * 100 + a[8] * 10 + a[9]) {
for (int i = 1; i <= 9; i++) printf("%d ", a[i]);
printf("\n");
}
return;
}
for (int i = 1; i <= n; i++) {
if (!st[i]) {
a[step] = i;
st[i] = true;
dfs(step + 1);
st[i] = false;
}
}
}
int main() {
dfs(1);
return 0;
}

View File

@@ -0,0 +1,67 @@
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 51;
int n, m; //n行m列
int p, q; //小哈所在的位置
int Min = INF; //步骤最小值
int a[N][N]; //地图
bool st[N][N]; //是否走过
/**
测试数据
5 4
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
1 1
4 3
*/
int dx[4] = {0, 1, 0, -1};//上,右,下,左
int dy[4] = {1, 0, -1, 0};
int tx, ty;
void dfs(int x, int y, int step) {
//判断是否到达小哈的位置
if (x == p && y == q) {
if (step < Min) Min = step; //更新最小值
return;
}
/*枚举四种走法*/
for (int i = 0; i <= 3; i++) {
/*计算下一个点的坐标*/
tx = x + dx[i], ty = y + dy[i];
if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
/*判断该点是否为障碍物或者已经在路径中*/
if (!a[tx][ty] && !st[tx][ty]) {
st[tx][ty] = true; //标记这个点已经走过
dfs(tx, ty, step + 1); //开始尝试下一个点
st[tx][ty] = false; //尝试结束,取消这个点的标记
}
}
}
int main() {
//小哼开始的位置
int sx, sy;
cin >> n >> m;
/*读入迷宫*/
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
//读入起点和终点坐标
cin >> sx >> sy >> p >> q;
/*从起点开始搜索*/
st[sx][sy] = true; //标记起点已经在路径中,防止后面重复走
//第一个参数是起点的x坐标,以此类推是起点的y坐标,初始步数为0
dfs(sx, sy, 0);
//输出最短步数
cout << Min << endl;
return 0;
}

View File

@@ -0,0 +1,44 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 30;
int a[N];
int n, k;
int cnt;
bool isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= n / i; i++)
if (n % i == 0) return false;
return true;
}
/**
* 功能:遍历每个卡片(数字),携带已经选择了几个的m参数
* @param step 第几个卡片
* @param sum 已经选择的数字总和
* @param m 已经选择了几个数字
* @return 可行方法数
*/
void dfs(int step, int sum, int m) {
//已经完成选择k个数,判断总和是不是质数.如果是质数,说明找到了一种合法解
if (m == k && isPrime(sum)) {
cnt++;
return;
}
//这句需在放在后面,这是因为如果是第n个摆放完的数字,满足上面的判断条件,也是可以的
if (step == n + 1) return;
//选择当前数字,总和增大a[step],同时个数增加了1
dfs(step + 1, sum + a[step], m + 1);
//放弃当前数字,总和不变,个数也不变
dfs(step + 1, sum, m);
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
dfs(1, 0, 0);
cout << cnt << endl;
return 0;
}

View File

@@ -0,0 +1,49 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 30;
const int INF = 0x3f3f3f3f;
int a[N];
bool st[N];
int n, k;
int cnt;
// vector<int> path;
bool isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= n / i; i++)
if (n % i == 0) return false;
return true;
}
void dfs(int step, int sum, int pre) {
if (step == k + 1) {
if (isPrime(sum)) {
cnt++;
// for (int i = 0; i < path.size(); i++) cout << path[i] << " ";
// cout << endl;
}
return;
}
for (int i = pre + 1; i <= n; i++)
if (!st[i]) {
st[i] = true;
// path.push_back(a[i]);
dfs(step + 1, sum + a[i], i);
// path.pop_back();
st[i] = false;
}
}
/*
4 3
3 7 12 19
3+7+19=29
答案:1
*/
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
dfs(1, 0, 0);
cout << cnt << endl;
return 0;
}

View File

@@ -0,0 +1,66 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 30;
const int INF = 0x3f3f3f3f;
int a[N];
bool st[N];
int n, k;
int cnt;
vector<int> path;
bool isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= n / i; i++)
if (n % i == 0) return false;
return true;
}
/**
* 功能:遍历每个箱子,向箱子里装卡片
* @param step 走在第几个箱子面前
* @param sum 已经获得的数字和
* @param start 从哪个数字可以开始进行选择
* 举个栗子:
* 3+7+19
* 3+19+7
* 7+3+19
* 7+19+3
* 19+3+7
* 19+7+3 这样就出来6个,肯定不行,需要考虑只出1个!
* 方法就是“从上一次选择的数字后面一位进行再次选择!”
* @return 获取可行方法数
*/
void dfs(int step, int sum, int start) {
//当走到第k+1个虚拟箱子面前,表示前面k个箱子已经装完
if (step == k + 1) {
if (isPrime(sum)) {
cnt++;
for (int i = 0; i < path.size(); i++)
cout << path[i] << " ";
cout << endl;
}
return;
}
//在没有走完箱子时,考虑当前箱子放哪张卡片
for (int i = start; i <= n; i++) {
if (!st[i]) {
st[i] = true;
path.push_back(i);
dfs(step + 1, sum + a[i], i + 1);
path.pop_back();
st[i] = false;
}
}
}
/*
4 3
3 7 12 19
答案:1
*/
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
dfs(1, 0, 1);
cout << cnt << endl;
return 0;
}

View File

@@ -0,0 +1,52 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 30;
const int INF = 0x3f3f3f3f;
int a[N];
bool st[N];
int n, k;
int cnt;
vector<int> path;
bool isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= n / i; i++)
if (n % i == 0) return false;
return true;
}
/**
* 功能:遍历每个箱子,向箱子里装卡片
* @param step 走在第几个箱子面前
* @param sum 已经获得的数字和
* @return 获取可行方法数
*/
void dfs(int step, int sum) {
//当走到第k+1个虚拟箱子面前,表示前面k个箱子已经装完
if (step == k + 1) {
if (isPrime(sum)) cnt++;
return;
}
//在没有走完箱子时,考虑当前箱子放哪张卡片
for (int i = path.size() == 0 ? 1 : path[path.size() - 1] + 1; i <= n; i++) {
if (!st[i]) {
st[i] = true;
path.push_back(i);
dfs(step + 1, sum + a[i]);
path.pop_back();
st[i] = false;
}
}
}
/*
4 3
3 7 12 19
答案:1
*/
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
dfs(1, 0);
cout << cnt << endl;
return 0;
}

View File

@@ -0,0 +1,34 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N];
int n, m;
int cnt;
/**
测试数据:
5 1
答案:
3
*/
void dfs(int step, int sum1, int sum2) {
//收集答案
if (step == n + 1) {
if (sum1 - sum2 == m) cnt++;
return;
}
//选择了当前的数字
dfs(step + 1, sum1 + a[step], sum2);
//放弃了当前的数字
dfs(step + 1, sum1, sum2 + a[step]);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) a[i] = i;
dfs(1, 0, 0);
cout << cnt << endl;
return 0;
}

View File

@@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const int INF = 0x3f3f3f3f;
int a[N];
int n;
int cnt;
/**
3
130 200 55
*/
void dfs(int step, int sum, int m) {
if (step == n + 1) {
cnt = max(cnt, sum);
return;
}
if (m > a[step]) {
dfs(step + 1, sum + 1, a[step]);
dfs(step + 1, sum, m);
} else
dfs(step + 1, sum, m);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i]; // 130 200
dfs(1, 0, INF);
printf("%d", cnt);
return 0;
}

View File

@@ -0,0 +1,33 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, k;
vector<int> path;
bool st[N];
void dfs(int step) {
if (step == k + 1) {
cout << "(";
for (int i = 0; i < path.size() - 1; ++i) {
cout << path[i] << ",";
}
cout << path.back() << ")";
return;
}
for (int i = 1; i <= n; i++) {
if (!st[i]) {
path.push_back(i);
st[i] = true;
dfs(step + 1);
st[i] = false;
path.pop_back();
}
}
}
int main() {
cin >> n >> k;
dfs(1);
return 0;
}

View File

@@ -0,0 +1,27 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int a[N];
int n, k;
vector<int> path;
void dfs(int step, int m) {
if (m == k) {
for (int i = 0; i < path.size(); i++) cout << path[i] << " ";
cout << endl;
return;
}
if (step == n + 1) return;
//选择了当前数字
path.push_back(a[step]);
dfs(step + 1, m + 1);
path.pop_back();
//放弃当前数字
dfs(step + 1, m);
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++)a[i] = i;
dfs(1, 0);
return 0;
}

View File

@@ -0,0 +1,24 @@
#include <bits/stdc++.h>
using namespace std;
int m, n;
int dx[] = {0, 1};
int dy[] = {1, 0};
int cnt;
void dfs(int x, int y) {
if (x == m && y == n) {
cnt++;
return;
}
for (int i = 0; i < 2; i++) {
int tx = x + dx[i], ty = y + dy[i];
if (tx <= m && ty <= n)
dfs(tx, ty);
}
}
int main() {
cin >> m >> n;
dfs(1, 1);
printf("%d", cnt);
return 0;
}

View File

@@ -0,0 +1,51 @@
#include <bits/stdc++.h>
using namespace std;
/**
测试用例:
8
输出结果:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2
*/
const int N = 30;
int a[N];
int st[N];
int n = 8;
//判断一个数是不是质数
bool isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= n / i; i++)
if (n % i == 0) return false;
return true;
}
void dfs(int step) {
if (step == n + 1) {
if (isPrime(a[n] + 1)) {
for (int i = 1; i <= n; i++)printf("%d ", a[i]);
printf("\n");
}
return;
}
for (int i = 2; i <= n; i++)
if (!st[i] && isPrime(a[step - 1] + i)) {
st[i] = true;
a[step] = i;
dfs(step + 1);
st[i] = false;
}
}
int main() {
//第一个位置写死,占上,不能改
a[1] = 1;
//开始深度优先搜索
dfs(2);
return 0;
}

23
TangDou/Dfs/DFS_1.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include <bits/stdc++.h>
using namespace std;
int n, m;
int ans;
int main() {
cin >> n >> m;
//出发点特判
if (n == 1 && m == 1) {
puts("1");
return 0;
}
//奇偶行分类,然后n = m特判
//如果n是奇数
if (n & 1) {
ans = n + m - 1;
if (n == m) ans--;
} else // n是偶数
ans = n + (m - 1) / 2 * 2;
printf("%d", ans);
return 0;
}

48
TangDou/Dfs/DFS_2.cpp Normal file
View File

@@ -0,0 +1,48 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
const int M = 30; //偏移量
int n, m; // n行m列
int ans[N][N];
int a[N]; //正对角线
int b[N]; //反对角线
void dfs(int x, int y, int sum) {
if (x == n + 1) {
ans[n][m] = max(ans[n][m], sum);
return;
}
//如果这个位置可以放,那么我们可以选择放
if (!a[x + y] && !b[x - y + M]) {
a[x + y] = 1;
b[x - y + M] = 1;
if (y == m)
dfs(x + 1, 1, sum + 1);
else
dfs(x, y + 1, sum + 1);
a[x + y] = 0;
b[x - y + M] = 0;
}
//不管可以不可以放,都可以选择不放
if (y == m)
dfs(x + 1, 1, sum);
else
dfs(x, y + 1, sum);
}
int main() {
//模拟7行7列,从(1,1)出发,开始打表,然后找规律
for (n = 1; n <= 7; n++)
for (m = 1; m <= 7; m++)
dfs(1, 1, 0);
//输出每个行、列情况下的摆法数量
for (int i = 1; i <= 7; i++) {
for (int j = 1; j <= 7; j++)
printf("%02d ", ans[i][j]);
puts("");
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 477 KiB

25
TangDou/Dfs/TD.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
// 快速幂
int qmi(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = (LL)res * a % mod;
a = (LL)a * a % mod;
b >>= 1;
}
return res;
}
int main() {
LL n;
cin >> n;
LL a = (n * (n + 1)) % mod;
a = a * (2 * n + 1) % mod;
a = a * qmi(6, mod - 2) % mod;
cout << a << endl;
return 0;
}

3
TangDou/Dfs/TD.in Normal file
View File

@@ -0,0 +1,3 @@
1 2 3
4 5 6
7 8 9

Binary file not shown.

After

Width:  |  Height:  |  Size: 235 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 316 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 322 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 240 KiB

46
TangDou/Dfs/题单.txt Normal file
View File

@@ -0,0 +1,46 @@
深度优先搜索dfs
1、数字全排列
https://www.cnblogs.com/littlehb/p/15094628.html
2、[][][] + [][][] = [][][] 问题
https://www.cnblogs.com/littlehb/p/15094628.html
3、解救小哈
https://www.cnblogs.com/littlehb/p/15117029.html
4、选数
https://www.luogu.com.cn/problem/P1036
n数中选择k个数,和为质数的方案数有多少?
4 3
3 7 12 19
5、蓝桥杯赛省赛第五题,小蓝突发奇想
6、蓝桥杯赛国赛第五题,投球游戏
7、取K个数
http://oj.521ma.cn/problem.php?id=1311
8、机器人走迷宫
file:///D:/python/S00309-STEMA%E8%80%83%E8%AF%95%E7%99%BD%E7%9A%AE%E4%B9%A6-v9.0-210630.pdf
127页第四题
9、素数环
http://oj.521ma.cn/problem.php?id=1312
10、排列的输出
11、整数的拆分
http://oj.521ma.cn/problem.php?id=1314
15、工作分配
http://oj.521ma.cn/problem.php?id=1316
12、第十届蓝桥杯赛省赛第五题,LQBS走迷宫
13、八皇后
http://oj.521ma.cn/problem.php?id=1313
14、传教士
http://oj.521ma.cn/problem.php?id=1315