diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..d413586 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,31 @@ +{ + "configurations": [ + { + "name": "C/C++: gcc.exe 构建和调试活动文件", + "type": "cppdbg", + "request": "launch", + "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", + "args": [], + "stopAtEntry": false, + "cwd": "${fileDirname}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe", + "setupCommands": [ + { + "description": "为 gdb 启用整齐打印", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "将反汇编风格设置为 Intel", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ], + "preLaunchTask": "C/C++: gcc.exe 生成活动文件" + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..cee8d6f --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,48 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++.exe 生成活动文件", + "command": "C:\\Program Files\\mingw64\\bin\\g++.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "调试器生成的任务。" + }, + { + "type": "cppbuild", + "label": "C/C++: gcc.exe 生成活动文件", + "command": "C:\\Program Files\\mingw64\\bin\\gcc.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": "build", + "detail": "调试器生成的任务。" + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/Algorithm/BackTracking/N-Queen-RemoveRepeated.cpp b/Algorithm/BackTracking/N-Queen-RemoveRepeated.cpp new file mode 100644 index 0000000..938fccb --- /dev/null +++ b/Algorithm/BackTracking/N-Queen-RemoveRepeated.cpp @@ -0,0 +1,216 @@ +#include + +using namespace std; +vector> solutions; + +string serialize(const vector& path) { + string s; + for (int x : path) s += char(x + '0'); + return s; +} + +// ַл vector +vector deserialize(const string& s) { + vector path; + for (char c : s) path.push_back(c - '0'); + return path; +} + +// ת 90㣨˳ʱ룩 +vector rotate90(const vector& path, int n) { + vector newPath(n); + for (int i = 0; i < n; i++) { + newPath[path[i]] = n - 1 - i; + } + return newPath; +} + +// ˮƽת +vector mirrorH(const vector& path, int n) { + vector newPath(n); + for (int i = 0; i < n; i++) newPath[i] = n - 1 - path[i]; + return newPath; +} + +// СĶԳƱʾ +string canonical(const vector& path, int n) { + vector forms; + vector cur = path; + + // 4 ת + ת + for (int k = 0; k < 4; k++) { + forms.push_back(serialize(cur)); + forms.push_back(serialize(mirrorH(cur, n))); + cur = rotate90(cur, n); + } + + return *min_element(forms.begin(), forms.end()); +} + +bool isValid(const vector& path, int row, int col) { + for (int i = 0; i < row; i++) { + int prevCol = path[i]; + if (prevCol == col) return false; // ͬгͻ + if (abs(row - i) == abs(col - prevCol)) return false; // Խ߳ͻ + } + return true; +} + +//path[i] = j; (i, j)ж +void dfs(int _n, vector& _path, int _i){ + if(_i == _n){ + solutions.push_back(_path); + return; + } + for(int j = 0; j < _n; j++){ + // + if(isValid(_path, _i, j)){ + _path[_i] = j; + //path + dfs(_n, _path, _i + 1); + //ָpath + _path[_i] = -1; + } + } +} + +int main(){ + //ȷÿʺλiͨƶ/ȷ + //ÿʺı־(i)ɵǴ1 - NвеĻʺ + int n; + cin >> n; + //vector> mat(n, vector(n, 0)); + vector path(n, -1); + dfs(n, path, 0); + for(auto x : solutions){ + for(auto y : x){ + cout << y << " "; + } + cout << endl; + } + cout << endl; + + set uniqueSolutions; + for (auto& sol : solutions) { + uniqueSolutions.insert(canonical(sol, n)); + } + + cout << "ʲͬ: " << uniqueSolutions.size() << endl; + + // ȥغĽ + for (auto& s : uniqueSolutions) { + vector path = deserialize(s); + for (int x : path) cout << x << " "; + cout << endl; + } + return 0; +} +/* +8 +0 4 7 5 2 6 1 3 +0 5 7 2 6 3 1 4 +0 6 3 5 7 1 4 2 +0 6 4 7 1 3 5 2 +1 3 5 7 2 0 6 4 +1 4 6 0 2 7 5 3 +1 4 6 3 0 7 5 2 +1 5 0 6 3 7 2 4 +1 5 7 2 0 3 6 4 +1 6 2 5 7 4 0 3 +1 6 4 7 0 3 5 2 +1 7 5 0 2 4 6 3 +2 0 6 4 7 1 3 5 +2 4 1 7 0 6 3 5 +2 4 1 7 5 3 6 0 +2 4 6 0 3 1 7 5 +2 4 7 3 0 6 1 5 +2 5 1 4 7 0 6 3 +2 5 1 6 0 3 7 4 +2 5 1 6 4 0 7 3 +2 5 3 0 7 4 6 1 +2 5 3 1 7 4 6 0 +2 5 7 0 3 6 4 1 +2 5 7 0 4 6 1 3 +2 5 7 1 3 0 6 4 +2 6 1 7 4 0 3 5 +2 6 1 7 5 3 0 4 +2 7 3 6 0 5 1 4 +3 0 4 7 1 6 2 5 +3 0 4 7 5 2 6 1 +3 1 4 7 5 0 2 6 +3 1 6 2 5 7 0 4 +3 1 6 2 5 7 4 0 +3 1 6 4 0 7 5 2 +3 1 7 4 6 0 2 5 +3 1 7 5 0 2 4 6 +3 5 0 4 1 7 2 6 +3 5 7 1 6 0 2 4 +3 5 7 2 0 6 4 1 +3 6 0 7 4 1 5 2 +3 6 2 7 1 4 0 5 +3 6 4 1 5 0 2 7 +3 6 4 2 0 5 7 1 +3 7 0 2 5 1 6 4 +3 7 0 4 6 1 5 2 +3 7 4 2 0 6 1 5 +4 0 3 5 7 1 6 2 +4 0 7 3 1 6 2 5 +4 0 7 5 2 6 1 3 +4 1 3 5 7 2 0 6 +4 1 3 6 2 7 5 0 +4 1 5 0 6 3 7 2 +4 1 7 0 3 6 2 5 +4 2 0 5 7 1 3 6 +4 2 0 6 1 7 5 3 +4 2 7 3 6 0 5 1 +4 6 0 2 7 5 3 1 +4 6 0 3 1 7 5 2 +4 6 1 3 7 0 2 5 +4 6 1 5 2 0 3 7 +4 6 1 5 2 0 7 3 +4 6 3 0 2 7 5 1 +4 7 3 0 2 5 1 6 +4 7 3 0 6 1 5 2 +5 0 4 1 7 2 6 3 +5 1 6 0 2 4 7 3 +5 1 6 0 3 7 4 2 +5 2 0 6 4 7 1 3 +5 2 0 7 3 1 6 4 +5 2 0 7 4 1 3 6 +5 2 4 6 0 3 1 7 +5 2 4 7 0 3 1 6 +5 2 6 1 3 7 0 4 +5 2 6 1 7 4 0 3 +5 2 6 3 0 7 1 4 +5 3 0 4 7 1 6 2 +5 3 1 7 4 6 0 2 +5 3 6 0 2 4 1 7 +5 3 6 0 7 1 4 2 +5 7 1 3 0 6 4 2 +6 0 2 7 5 3 1 4 +6 1 3 0 7 4 2 5 +6 1 5 2 0 3 7 4 +6 2 0 5 7 4 1 3 +6 2 7 1 4 0 5 3 +6 3 1 4 7 0 2 5 +6 3 1 7 5 0 2 4 +6 4 2 0 5 7 1 3 +7 1 3 0 6 4 2 5 +7 1 4 2 0 6 3 5 +7 2 0 5 1 4 6 3 +7 3 0 2 5 1 6 4 + +ʲͬ: 12 +0 4 7 5 2 6 1 3 +0 5 7 2 6 3 1 4 +1 3 5 7 2 0 6 4 +1 4 6 0 2 7 5 3 +1 4 6 3 0 7 5 2 +1 5 0 6 3 7 2 4 +1 5 7 2 0 3 6 4 +1 6 2 5 7 4 0 3 +1 6 4 7 0 3 5 2 +2 4 1 7 0 6 3 5 +2 4 7 3 0 6 1 5 +2 5 1 4 7 0 6 3 +*/ diff --git a/Algorithm/BackTracking/N-Queen.cpp b/Algorithm/BackTracking/N-Queen.cpp new file mode 100644 index 0000000..f3a389c --- /dev/null +++ b/Algorithm/BackTracking/N-Queen.cpp @@ -0,0 +1,146 @@ +#include +#include +using namespace std; +vector> result; + +bool isValid(const vector& path, int row, int col) { + for (int i = 0; i < row; i++) { + int prevCol = path[i]; + if (prevCol == col) return false; // ͬгͻ + if (abs(row - i) == abs(col - prevCol)) return false; // Խ߳ͻ + } + return true; +} + +//path[i] = j; (i, j)ж +void dfs(int _n, vector& _path, int _i){ + if(_i == _n){ + result.push_back(_path); + return; + } + for(int j = 0; j < _n; j++){ + // + if(isValid(_path, _i, j)){ + _path[_i] = j; + //path + dfs(_n, _path, _i + 1); + //ָpath + _path[_i] = -1; + } + + + } +} + +int main(){ + //ȷÿʺλiͨƶ/ȷ + //ÿʺı־(i)ɵǴ1 - NвеĻʺ + int n; + cin >> n; + //vector> mat(n, vector(n, 0)); + vector path(n, -1); + dfs(n, path, 0); + for(auto x : result){ + for(auto y : x){ + cout << y << " "; + } + cout << endl; + } + cout << endl; + return 0; +} +/* +8 +0 4 7 5 2 6 1 3 +0 5 7 2 6 3 1 4 +0 6 3 5 7 1 4 2 +0 6 4 7 1 3 5 2 +1 3 5 7 2 0 6 4 +1 4 6 0 2 7 5 3 +1 4 6 3 0 7 5 2 +1 5 0 6 3 7 2 4 +1 5 7 2 0 3 6 4 +1 6 2 5 7 4 0 3 +1 6 4 7 0 3 5 2 +1 7 5 0 2 4 6 3 +2 0 6 4 7 1 3 5 +2 4 1 7 0 6 3 5 +2 4 1 7 5 3 6 0 +2 4 6 0 3 1 7 5 +2 4 7 3 0 6 1 5 +2 5 1 4 7 0 6 3 +2 5 1 6 0 3 7 4 +2 5 1 6 4 0 7 3 +2 5 3 0 7 4 6 1 +2 5 3 1 7 4 6 0 +2 5 7 0 3 6 4 1 +2 5 7 0 4 6 1 3 +2 5 7 1 3 0 6 4 +2 6 1 7 4 0 3 5 +2 6 1 7 5 3 0 4 +2 7 3 6 0 5 1 4 +3 0 4 7 1 6 2 5 +3 0 4 7 5 2 6 1 +3 1 4 7 5 0 2 6 +3 1 6 2 5 7 0 4 +3 1 6 2 5 7 4 0 +3 1 6 4 0 7 5 2 +3 1 7 4 6 0 2 5 +3 1 7 5 0 2 4 6 +3 5 0 4 1 7 2 6 +3 5 7 1 6 0 2 4 +3 5 7 2 0 6 4 1 +3 6 0 7 4 1 5 2 +3 6 2 7 1 4 0 5 +3 6 4 1 5 0 2 7 +3 6 4 2 0 5 7 1 +3 7 0 2 5 1 6 4 +3 7 0 4 6 1 5 2 +3 7 4 2 0 6 1 5 +4 0 3 5 7 1 6 2 +4 0 7 3 1 6 2 5 +4 0 7 5 2 6 1 3 +4 1 3 5 7 2 0 6 +4 1 3 6 2 7 5 0 +4 1 5 0 6 3 7 2 +4 1 7 0 3 6 2 5 +4 2 0 5 7 1 3 6 +4 2 0 6 1 7 5 3 +4 2 7 3 6 0 5 1 +4 6 0 2 7 5 3 1 +4 6 0 3 1 7 5 2 +4 6 1 3 7 0 2 5 +4 6 1 5 2 0 3 7 +4 6 1 5 2 0 7 3 +4 6 3 0 2 7 5 1 +4 7 3 0 2 5 1 6 +4 7 3 0 6 1 5 2 +5 0 4 1 7 2 6 3 +5 1 6 0 2 4 7 3 +5 1 6 0 3 7 4 2 +5 2 0 6 4 7 1 3 +5 2 0 7 3 1 6 4 +5 2 0 7 4 1 3 6 +5 2 4 6 0 3 1 7 +5 2 4 7 0 3 1 6 +5 2 6 1 3 7 0 4 +5 2 6 1 7 4 0 3 +5 2 6 3 0 7 1 4 +5 3 0 4 7 1 6 2 +5 3 1 7 4 6 0 2 +5 3 6 0 2 4 1 7 +5 3 6 0 7 1 4 2 +5 7 1 3 0 6 4 2 +6 0 2 7 5 3 1 4 +6 1 3 0 7 4 2 5 +6 1 5 2 0 3 7 4 +6 2 0 5 7 4 1 3 +6 2 7 1 4 0 5 3 +6 3 1 4 7 0 2 5 +6 3 1 7 5 0 2 4 +6 4 2 0 5 7 1 3 +7 1 3 0 6 4 2 5 +7 1 4 2 0 6 3 5 +7 2 0 5 1 4 6 3 +7 3 0 2 5 1 6 4 +*/ diff --git a/Algorithm/BackTracking/UVa129-Krypton-Factor.cpp b/Algorithm/BackTracking/UVa129-Krypton-Factor.cpp new file mode 100644 index 0000000..8431d9c --- /dev/null +++ b/Algorithm/BackTracking/UVa129-Krypton-Factor.cpp @@ -0,0 +1,6 @@ +#include +using namespace std; +int main(){ + + return 0; +} diff --git a/Algorithm/BackTracking/UVa524-PrimeRingProblem.cpp b/Algorithm/BackTracking/UVa524-PrimeRingProblem.cpp new file mode 100644 index 0000000..f1f4638 --- /dev/null +++ b/Algorithm/BackTracking/UVa524-PrimeRingProblem.cpp @@ -0,0 +1,59 @@ +#include +#include +using namespace std; +vector> result; +bool prime(int x){ + if(x <= 1) return false; + for(int i = 2; i * i <= x; i++){ + if(x % i == 0) return false; + } + return true; +} + + +bool checkPrime(int n, vector& x) { + if (x.size() < 2) return true; // 2ü + + int tmp = x[x.size() - 1] + x[x.size() - 2]; + if (!prime(tmp)) return false; + + if (x.size() == n) { + if (!prime(x.back() + x[0])) return false; + } + return true; +} + + +void dfs(int n, vector& _path, vector& _used){ + if(!checkPrime(n, _path)) return; + if(_path.size() == n){ + result.push_back(_path); + return; + } + for(int i = 0; i < n; i++){ + if(!_used[i]){ + _used[i] = true; + _path.push_back(i + 1); + dfs(n, _path, _used); + _used[i] = false; + _path.pop_back(); + } + + } +} +int main(){ + int n; + cin >> n; + vector used(n, false); + vector path; + dfs(n, path, used); + for(auto x : result){ + for(auto y : x){ + cout << y << " "; + } + cout << endl; + } + cout << endl; + system("pause"); + return 0; +} diff --git a/Algorithm/DS-Related/Graph/DFS/UVa673-Parentheses-Balance([]).cpp b/Algorithm/DS-Related/Graph/DFS/UVa673-Parentheses-Balance([]).cpp new file mode 100644 index 0000000..8fc6a82 --- /dev/null +++ b/Algorithm/DS-Related/Graph/DFS/UVa673-Parentheses-Balance([]).cpp @@ -0,0 +1,40 @@ +#include +#include +using namespace std; +//确定每个节点要干什么 +bool dfs(const string& s, int l, int r) { + if (l > r) return true; // 空串平衡 + if (l == r) return false; // 单个字符不平衡 + + // 必须匹配类型 + char open = s[l]; + char close = (open == '(') ? ')' : (open == '[') ? ']' : 0; + if (close == 0) return false; // 非括号字符 + + int cnt = 0; + int p = -1; + for (int i = l; i <= r; i++) { + if (s[i] == '(' || s[i] == '[') cnt++; + if (s[i] == ')' || s[i] == ']') cnt--; + if (cnt == 0) { + p = i; // 找到匹配的右括号 + break; + } + } + + if (p == -1) return false; // 没找到匹配 + if (s[p] != close) return false; // 类型不匹配(第一个和最后一个字符匹配) + + // 内部和剩余区间递归 + return dfs(s, l + 1, p - 1) && dfs(s, p + 1, r); +} + +int main(){ + string a; + while(true) + { + cin >> a; + dfs(a, 0, a.size() - 1) ? cout << "YES" << endl : cout << "NO" << endl; + } + return 0; +} \ No newline at end of file diff --git a/Algorithm/DS-Related/Tree/QuadTree/UVa806-Spatial-Structure.cpp b/Algorithm/DS-Related/Tree/QuadTree/UVa806-Spatial-Structure.cpp new file mode 100644 index 0000000..d3184a4 --- /dev/null +++ b/Algorithm/DS-Related/Tree/QuadTree/UVa806-Spatial-Structure.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +using namespace std; +vector result; + +int _5to10(const vector& x){ + int num = 0; + for(int i = x.size() - 1; i >= 0; i--){ + num = num * 5 + x[i]; + } + return num; +} + +//Tȡboolint[0, 1]дһ4 +template +struct Node{ + T element; + bool isLeaf; + Node* NW;//1 + Node* NE;//2 + Node* SW;//3 + Node* SE;//4 + vector path; + + Node(const T& _element) : isLeaf(true), element(_element), NW(nullptr), NE(nullptr), SW(nullptr), SE(nullptr) {} +}; + +//ﲻҪTreeֱõݹ +//Խߵ(i, j) +template +void dfs(vector>& mat, Node*& node, vector& path, int li, int lj, int ri, int rj) { + int val = mat[li][lj]; + bool same = true; + for (int i = li; i <= ri && same; i++) { + for (int j = lj; j <= rj && same; j++) { + if (mat[i][j] != val) same = false; + } + } + + if (same) { // Ҷ + node = new Node(val); + node->path = path; + + //ת10 + result.push_back(_5to10(node->path)); + return; + } + + node = new Node(-1); // ڲڵ + node->isLeaf = false; + + int midRow = (li + ri) / 2; + int midCol = (lj + rj) / 2; + + // NW=1 + path.push_back(1); + dfs(mat, node->NW, path, li, lj, midRow, midCol); + path.pop_back(); + + // NE=2 + path.push_back(2); + dfs(mat, node->NE, path, li, midCol+1, midRow, rj); + path.pop_back(); + + // SW=3 + path.push_back(3); + dfs(mat, node->SW, path, midRow+1, lj, ri, midCol); + path.pop_back(); + + // SE=4 + path.push_back(4); + dfs(mat, node->SE, path, midRow+1, midCol+1, ri, rj); + path.pop_back(); + +} + +int main(){ + int n, m; + cin >> n >> m; + vector> matrix(n, vector(m)); + for(int i = 0; i < n; i++) + for(int j = 0; j < m; j++) + cin >> matrix[i][j]; + + Node* root = nullptr; + vector path; + dfs(matrix, root, path, 0, 0, n-1, m-1); + + sort(result.begin(), result.end()); + for (int v : result) cout << v << " "; + // TODO: Ҷӽڵ㣬 path תΪʮ + return 0; +} +/* +8 8 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 1 1 1 1 +0 0 0 0 1 1 1 1 +0 0 0 1 1 1 1 1 +0 0 1 1 1 1 1 1 +0 0 1 1 1 1 0 0 +0 0 1 1 1 0 0 0 +1 7 8 9 12 14 17 18 22 23 24 38 44 63 69 88 94 113 119 + +*/ diff --git a/Algorithm/Enumerate/HuaRongDao-or-Eight-Number-BFS-YES.cpp b/Algorithm/Enumerate/HuaRongDao-or-Eight-Number-BFS-YES.cpp new file mode 100644 index 0000000..aabb84d --- /dev/null +++ b/Algorithm/Enumerate/HuaRongDao-or-Eight-Number-BFS-YES.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +using namespace std; +set> visited; +//ĬϾŹ +void swapUp(vector& _v, int _i){ + if(_i == 0 || _i == 1 || _i ==2 ) return; + swap(_v[_i], _v[_i - 3]); +} +void swapDown(vector& _v, int _i){ + if(_i == 6 || _i == 7 || _i ==8 ) return; + swap(_v[_i], _v[_i + 3]); +} +void swapLeft(vector& _v, int _i){ + if(_i == 0 || _i == 3 || _i ==6 ) return; + swap(_v[_i], _v[_i - 1]); +} +void swapRight(vector& _v, int _i){ + if(_i == 2 || _i == 5 || _i ==8 ) return; + swap(_v[_i], _v[_i + 1]); +} +//pos -> ׸λ +int bfs(vector _aim, vector _v){ + deque, int>> q; + q.push_back({_v, 0}); + visited.insert(_v); + + while(!q.empty()){ + auto tmp = q.front(); q.pop_front(); + vector tmpv = tmp.first; + int step = tmp.second; + + if(tmpv == _aim) return step; + + int x; + for(int i = 0; i < tmpv.size(); i++){ + if(tmpv[i] == 0) { + x = i; + break; + } + } + // + if(x >= 3){ + vector tmps = tmpv; + swapUp(tmps, x); + if(!visited.count(tmps)){ + visited.insert(tmps); + q.push_back({tmps, step + 1}); + } + } + + // + if(x <= 5){ + vector tmps = tmpv; + swapDown(tmps, x); + if(!visited.count(tmps)){ + visited.insert(tmps); + q.push_back({tmps, step + 1}); + } + } + + // + if(x % 3 != 0){ + vector tmps = tmpv; + swapLeft(tmps, x); + if(!visited.count(tmps)){ + visited.insert(tmps); + q.push_back({tmps, step + 1}); + } + } + + // + if(x % 3 != 2){ + vector tmps = tmpv; + swapRight(tmps, x); + if(!visited.count(tmps)){ + visited.insert(tmps); + q.push_back({tmps, step + 1}); + } + } + } + return -1; // ޽ +} + +int main(){ + int n; + cin >> n; + vector arr(n, -1); + vector aim(n, -1); + int pos; + for(int i = 0; i < n; i++){ + cin >> arr[i]; + if(arr[i] == 0) pos = i; + } + for(int i = 0; i < n; i++){ + cin >> aim[i]; + } + cout << bfs(aim, arr) << endl; + + return 0; +} +/* +9 +2 6 4 1 3 7 0 5 8 +8 1 5 7 3 6 4 0 2 +*/ diff --git a/Algorithm/Enumerate/HuaRongDao-or-Eight-Number-DFS-NO.cpp b/Algorithm/Enumerate/HuaRongDao-or-Eight-Number-DFS-NO.cpp new file mode 100644 index 0000000..a2eeb60 --- /dev/null +++ b/Algorithm/Enumerate/HuaRongDao-or-Eight-Number-DFS-NO.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +using namespace std; +set> visited; +//ĬϾŹ +void swapUp(vector& _v, int _i){ + if(_i == 0 || _i == 1 || _i ==2 ) return; + swap(_v[_i], _v[_i - 3]); +} +void swapDown(vector& _v, int _i){ + if(_i == 6 || _i == 7 || _i ==8 ) return; + swap(_v[_i], _v[_i + 3]); +} +void swapLeft(vector& _v, int _i){ + if(_i == 0 || _i == 3 || _i ==6 ) return; + swap(_v[_i], _v[_i - 1]); +} +void swapRight(vector& _v, int _i){ + if(_i == 2 || _i == 5 || _i ==8 ) return; + swap(_v[_i], _v[_i + 1]); +} +//x -> ׸λ +void dfs(vector _aim, vector _v, int step, int x){ + if(visited.count(_v)) return; // ظ״̬ + visited.insert(_v); + // + if(x >= 3){ + vector tmp = _v; + swapUp(tmp, x); + dfs(_aim, tmp, step + 1, x - 3); + } + + // + if(x <= 5){ + vector tmp = _v; + swapDown(tmp, x); + dfs(_aim, tmp, step + 1, x + 3); + } + + // + if(x % 3 != 0){ + vector tmp = _v; + swapLeft(tmp, x); + dfs(_aim, tmp, step + 1, x - 1); + } + + // + if(x % 3 != 2){ + vector tmp = _v; + swapRight(tmp, x); + dfs(_aim, tmp, step + 1, x + 1); + } + //Ƚ + if(_aim == _v){ + cout << step << " "; + return; + } + +} + +int main(){ + int n; + cin >> n; + vector arr(n, -1); + vector aim(n, -1); + int pos; + for(int i = 0; i < n; i++){ + cin >> arr[i]; + if(arr[i] == 0) pos = i; + } + for(int i = 0; i < n; i++){ + cin >> aim[i]; + } + dfs(aim, arr, 0, pos); + + return 0; +} +/* +9 +2 6 4 1 3 7 0 5 8 +8 1 5 7 3 6 4 0 2 +*/ diff --git a/Algorithm/Enumerate/all_permutation_dfs.cpp b/Algorithm/Enumerate/all_permutation_dfs.cpp new file mode 100644 index 0000000..3e70e61 --- /dev/null +++ b/Algorithm/Enumerate/all_permutation_dfs.cpp @@ -0,0 +1,30 @@ +#include +#include +using namespace std; + +template +void dfs(vector& _path, vector& _used, int n) { + if(_path.size() == n){ + for(auto x : _path) cout << x << " "; + cout << endl; + return; + } + for(int i = 0; i < n; i++){ + if(!_used[i]) { + _used[i] = true; + _path.push_back(i + 1); + dfs(_path, _used, n); + _path.pop_back(); + _used[i] = false; + } + } +} + +int main(){ + int n; + cin >> n; + vector path; + vector used(n, false); + dfs(path, used, n); + return 0; +} diff --git a/Algorithm/Enumerate/all_permutation_dfs_all_used.cpp b/Algorithm/Enumerate/all_permutation_dfs_all_used.cpp new file mode 100644 index 0000000..60bd66b --- /dev/null +++ b/Algorithm/Enumerate/all_permutation_dfs_all_used.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +template +void dfs(vector& _path, int n) { + if(_path.size() == n){ + for(auto x : _path) cout << x << " "; + cout << endl; + return; + } + for(int i = 0; i < n; i++){ + _path.push_back(i + 1); + dfs(_path, n); + _path.pop_back(); + } +} + +int main(){ + int n; + cin >> n; + vector path; + dfs(path, n); + return 0; +} diff --git a/Algorithm/Enumerate/all_subset.cpp b/Algorithm/Enumerate/all_subset.cpp new file mode 100644 index 0000000..505633e --- /dev/null +++ b/Algorithm/Enumerate/all_subset.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; + +template +void dfs(vector& _path, vector& _used, int n) { + if(_path.size() == n){ + for(auto x : _path) cout << x << " "; + cout << endl; + return; + } + for(int i = 0; i < n; i++){ + if(!_used[i]) { + _used[i] = true; + _path.push_back(i + 1); + dfs(_path, _used, n); + _path.pop_back(); + _used[i] = false; + } + } + for(auto x : _path) cout << x << " "; + cout << endl; +} + +int main(){ + int n; + cin >> n; + vector path; + vector used(n, false); + dfs(path, used, n); + return 0; +} diff --git a/Algorithm/Maths/P1102 A-B数对/P1102 A-B 数对 unordered_map.exe b/Algorithm/Maths/P1102 A-B数对/P1102 A-B 数对 unordered_map.exe deleted file mode 100644 index 9e83ce8..0000000 Binary files a/Algorithm/Maths/P1102 A-B数对/P1102 A-B 数对 unordered_map.exe and /dev/null differ diff --git a/Algorithm/Simulate/P9748小苹果.cpp b/Algorithm/Simulate/P9748小苹果.cpp new file mode 100644 index 0000000..bfcf51b --- /dev/null +++ b/Algorithm/Simulate/P9748小苹果.cpp @@ -0,0 +1,21 @@ +#include +#include +using namespace std; +int main(){ + int n; + cin >> n; + vector a(n - 1, 0); + a.push_back(1); + int idx = 0; + int js = 0; + int p; + while(!a.empty()){ + if(idx >= n) idx = 0; + if(a[idx] == 1) p = js; + a.erase(a.begin() + idx); + idx = idx + 2; + js++; + } + cout << js << " " << p << endl; + return 0; +} \ No newline at end of file