Altered Algorithm
This commit is contained in:
		
							
								
								
									
										31
									
								
								.vscode/launch.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								.vscode/launch.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -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"
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										48
									
								
								.vscode/tasks.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								.vscode/tasks.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -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"
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										216
									
								
								Algorithm/BackTracking/N-Queen-RemoveRepeated.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										216
									
								
								Algorithm/BackTracking/N-Queen-RemoveRepeated.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,216 @@
 | 
			
		||||
#include <bits/stdc++.h>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
vector<vector<int>> solutions; 
 | 
			
		||||
 | 
			
		||||
string serialize(const vector<int>& path) {
 | 
			
		||||
    string s;
 | 
			
		||||
    for (int x : path) s += char(x + '0');
 | 
			
		||||
    return s;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD> vector<int>
 | 
			
		||||
vector<int> deserialize(const string& s) {
 | 
			
		||||
    vector<int> path;
 | 
			
		||||
    for (char c : s) path.push_back(c - '0');
 | 
			
		||||
    return path;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת 90<39>㣨˳ʱ<CBB3>룩<EFBFBD><EBA3A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
			
		||||
vector<int> rotate90(const vector<int>& path, int n) {
 | 
			
		||||
    vector<int> newPath(n);
 | 
			
		||||
    for (int i = 0; i < n; i++) {
 | 
			
		||||
        newPath[path[i]] = n - 1 - i;
 | 
			
		||||
    }
 | 
			
		||||
    return newPath;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˮƽ<CBAE><C6BD>ת<EFBFBD><D7AA>
 | 
			
		||||
vector<int> mirrorH(const vector<int>& path, int n) {
 | 
			
		||||
    vector<int> newPath(n);
 | 
			
		||||
    for (int i = 0; i < n; i++) newPath[i] = n - 1 - path[i];
 | 
			
		||||
    return newPath;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>ĶԳƱ<D4B3>ʾ
 | 
			
		||||
string canonical(const vector<int>& path, int n) {
 | 
			
		||||
    vector<string> forms;
 | 
			
		||||
    vector<int> cur = path;
 | 
			
		||||
 | 
			
		||||
    // 4 <20><><EFBFBD><EFBFBD>ת + <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
 | 
			
		||||
    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<int>& path, int row, int col) {
 | 
			
		||||
    for (int i = 0; i < row; i++) {
 | 
			
		||||
        int prevCol = path[i];
 | 
			
		||||
        if (prevCol == col) return false; // ͬ<>г<EFBFBD>ͻ
 | 
			
		||||
        if (abs(row - i) == abs(col - prevCol)) return false; // <20>Խ<EFBFBD><D4BD>߳<EFBFBD>ͻ
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//path[i] = j; (i, j)<29><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD> 
 | 
			
		||||
void dfs(int _n, vector<int>& _path, int _i){	
 | 
			
		||||
	if(_i == _n){
 | 
			
		||||
		solutions.push_back(_path);
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
	for(int j = 0; j < _n; j++){
 | 
			
		||||
			//<2F><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
 | 
			
		||||
			if(isValid(_path, _i, j)){
 | 
			
		||||
				_path[_i] = j;
 | 
			
		||||
				//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>path 
 | 
			
		||||
				dfs(_n, _path, _i + 1);
 | 
			
		||||
				//<2F>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>path 
 | 
			
		||||
				_path[_i] = -1;
 | 
			
		||||
			}
 | 
			
		||||
	} 
 | 
			
		||||
} 
 | 
			
		||||
 | 
			
		||||
int main(){
 | 
			
		||||
	//<2F><><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>ʺ<EFBFBD>λi<CEBB><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>/<2F><><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7>
 | 
			
		||||
	//ÿ<><C3BF><EFBFBD>ʺ<EFBFBD><CABA>ı<EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD>(i)<29><><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǵ<EFBFBD>1 - N<>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĻʺ<C4BB> 
 | 
			
		||||
	int n;
 | 
			
		||||
	cin >> n;
 | 
			
		||||
	//vector<vector<int>> mat(n, vector<int>(n, 0));
 | 
			
		||||
	vector<int> path(n, -1);
 | 
			
		||||
	dfs(n, path, 0); 
 | 
			
		||||
	for(auto x : solutions){
 | 
			
		||||
		for(auto y : x){
 | 
			
		||||
			cout << y << " ";
 | 
			
		||||
		}
 | 
			
		||||
		cout << endl;
 | 
			
		||||
	}
 | 
			
		||||
	cout << endl;
 | 
			
		||||
	
 | 
			
		||||
	set<string> uniqueSolutions;
 | 
			
		||||
    for (auto& sol : solutions) {
 | 
			
		||||
        uniqueSolutions.insert(canonical(sol, n));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cout << "<EFBFBD><EFBFBD><EFBFBD>ʲ<EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: " << uniqueSolutions.size() << endl;
 | 
			
		||||
 | 
			
		||||
    // <20><><EFBFBD><EFBFBD>ȥ<EFBFBD>غ<EFBFBD><D8BA>Ľ<EFBFBD>
 | 
			
		||||
    for (auto& s : uniqueSolutions) {
 | 
			
		||||
        vector<int> 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
 | 
			
		||||
 | 
			
		||||
<EFBFBD><EFBFBD><EFBFBD>ʲ<EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: 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
 | 
			
		||||
*/
 | 
			
		||||
							
								
								
									
										146
									
								
								Algorithm/BackTracking/N-Queen.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										146
									
								
								Algorithm/BackTracking/N-Queen.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,146 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
using namespace std;
 | 
			
		||||
vector<vector<int>> result; 
 | 
			
		||||
 | 
			
		||||
bool isValid(const vector<int>& path, int row, int col) {
 | 
			
		||||
    for (int i = 0; i < row; i++) {
 | 
			
		||||
        int prevCol = path[i];
 | 
			
		||||
        if (prevCol == col) return false; // ͬ<>г<EFBFBD>ͻ
 | 
			
		||||
        if (abs(row - i) == abs(col - prevCol)) return false; // <20>Խ<EFBFBD><D4BD>߳<EFBFBD>ͻ
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//path[i] = j; (i, j)<29><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD> 
 | 
			
		||||
void dfs(int _n, vector<int>& _path, int _i){	
 | 
			
		||||
	if(_i == _n){
 | 
			
		||||
		result.push_back(_path);
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
	for(int j = 0; j < _n; j++){
 | 
			
		||||
			//<2F><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
 | 
			
		||||
			if(isValid(_path, _i, j)){
 | 
			
		||||
				_path[_i] = j;
 | 
			
		||||
				//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>path 
 | 
			
		||||
				dfs(_n, _path, _i + 1);
 | 
			
		||||
				//<2F>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>path 
 | 
			
		||||
				_path[_i] = -1;
 | 
			
		||||
			}
 | 
			
		||||
			
 | 
			
		||||
			
 | 
			
		||||
	} 
 | 
			
		||||
} 
 | 
			
		||||
 | 
			
		||||
int main(){
 | 
			
		||||
	//<2F><><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>ʺ<EFBFBD>λi<CEBB><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>/<2F><><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7>
 | 
			
		||||
	//ÿ<><C3BF><EFBFBD>ʺ<EFBFBD><CABA>ı<EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD>(i)<29><><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǵ<EFBFBD>1 - N<>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĻʺ<C4BB> 
 | 
			
		||||
	int n;
 | 
			
		||||
	cin >> n;
 | 
			
		||||
	//vector<vector<int>> mat(n, vector<int>(n, 0));
 | 
			
		||||
	vector<int> 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
 | 
			
		||||
*/
 | 
			
		||||
							
								
								
									
										6
									
								
								Algorithm/BackTracking/UVa129-Krypton-Factor.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								Algorithm/BackTracking/UVa129-Krypton-Factor.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
using namespace std;
 | 
			
		||||
int main(){
 | 
			
		||||
	
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										59
									
								
								Algorithm/BackTracking/UVa524-PrimeRingProblem.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								Algorithm/BackTracking/UVa524-PrimeRingProblem.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,59 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
using namespace std;
 | 
			
		||||
vector<vector<int>> 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<int>& x) {
 | 
			
		||||
    if (x.size() < 2) return true; // <20><><EFBFBD><EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><C3BC><EFBFBD>
 | 
			
		||||
 | 
			
		||||
    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<int>& _path, vector<bool>& _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<bool> used(n, false);
 | 
			
		||||
	vector<int> path;
 | 
			
		||||
	dfs(n, path, used);
 | 
			
		||||
	for(auto x : result){
 | 
			
		||||
		for(auto y : x){
 | 
			
		||||
			cout << y << " ";
 | 
			
		||||
		}
 | 
			
		||||
		cout << endl;
 | 
			
		||||
	}
 | 
			
		||||
	cout << endl;
 | 
			
		||||
	system("pause");
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,40 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <string>
 | 
			
		||||
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;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										108
									
								
								Algorithm/DS-Related/Tree/QuadTree/UVa806-Spatial-Structure.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								Algorithm/DS-Related/Tree/QuadTree/UVa806-Spatial-Structure.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,108 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <vector> 
 | 
			
		||||
using namespace std;
 | 
			
		||||
vector<int> result;
 | 
			
		||||
 | 
			
		||||
int _5to10(const vector<int>& x){
 | 
			
		||||
	int num = 0;
 | 
			
		||||
	for(int i = x.size() - 1; i >= 0; i--){
 | 
			
		||||
		num = num * 5 + x[i];
 | 
			
		||||
	}
 | 
			
		||||
	return num;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//Tȡbool<6F><6C><EFBFBD><EFBFBD>int[0, 1]<5D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBC>дһ<D0B4><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 
 | 
			
		||||
template <class T>
 | 
			
		||||
struct Node{
 | 
			
		||||
	T element;
 | 
			
		||||
	bool isLeaf;
 | 
			
		||||
	Node<T>* NW;//1
 | 
			
		||||
	Node<T>* NE;//2
 | 
			
		||||
	Node<T>* SW;//3
 | 
			
		||||
	Node<T>* SE;//4
 | 
			
		||||
	vector<int> path;
 | 
			
		||||
	
 | 
			
		||||
	Node(const T& _element) : isLeaf(true), element(_element), NW(nullptr), NE(nullptr), SW(nullptr), SE(nullptr) {} 
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
//<2F><><EFBFBD>ﲻ<EFBFBD><EFB2BB>ҪTree<65><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD>õݹ<C3B5><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>  
 | 
			
		||||
//<2F><><EFBFBD>Խ<EFBFBD><D4BD>ߵ<EFBFBD>(i, j) 
 | 
			
		||||
template <class T>
 | 
			
		||||
void dfs(vector<vector<int>>& mat, Node<T>*& node, vector<int>& 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) {  // Ҷ<><D2B6>
 | 
			
		||||
        node = new Node<T>(val);
 | 
			
		||||
        node->path = path;
 | 
			
		||||
        
 | 
			
		||||
        //ת<><D7AA>10<31><30><EFBFBD><EFBFBD> 
 | 
			
		||||
        result.push_back(_5to10(node->path));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    node = new Node<T>(-1); // <20>ڲ<EFBFBD><DAB2>ڵ<EFBFBD>
 | 
			
		||||
    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<vector<int>> matrix(n, vector<int>(m));
 | 
			
		||||
    for(int i = 0; i < n; i++)
 | 
			
		||||
        for(int j = 0; j < m; j++)
 | 
			
		||||
            cin >> matrix[i][j];
 | 
			
		||||
 | 
			
		||||
    Node<int>* root = nullptr;
 | 
			
		||||
    vector<int> path;
 | 
			
		||||
    dfs(matrix, root, path, 0, 0, n-1, m-1);
 | 
			
		||||
 | 
			
		||||
	sort(result.begin(), result.end());
 | 
			
		||||
	for (int v : result) cout << v << " ";
 | 
			
		||||
    // TODO: <20><><EFBFBD><EFBFBD>Ҷ<EFBFBD>ӽڵ㣬<DAB5><E3A3AC> path ת<><D7AA>Ϊʮ<CEAA><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
*/
 | 
			
		||||
							
								
								
									
										109
									
								
								Algorithm/Enumerate/HuaRongDao-or-Eight-Number-BFS-YES.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								Algorithm/Enumerate/HuaRongDao-or-Eight-Number-BFS-YES.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,109 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <deque> 
 | 
			
		||||
#include <set>
 | 
			
		||||
#include <utility>
 | 
			
		||||
using namespace std;
 | 
			
		||||
set<vector<int>> visited;
 | 
			
		||||
//<2F><><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϾŹ<CFBE><C5B9><EFBFBD> 
 | 
			
		||||
void swapUp(vector<int>& _v, int _i){
 | 
			
		||||
	if(_i == 0 || _i == 1 || _i ==2 ) return;
 | 
			
		||||
	swap(_v[_i], _v[_i - 3]);
 | 
			
		||||
}
 | 
			
		||||
void swapDown(vector<int>& _v, int _i){
 | 
			
		||||
	if(_i == 6 || _i == 7 || _i ==8 ) return;
 | 
			
		||||
	swap(_v[_i], _v[_i + 3]);
 | 
			
		||||
}
 | 
			
		||||
void swapLeft(vector<int>& _v, int _i){
 | 
			
		||||
	if(_i == 0 || _i == 3 || _i ==6 ) return;
 | 
			
		||||
	swap(_v[_i], _v[_i - 1]);
 | 
			
		||||
}
 | 
			
		||||
void swapRight(vector<int>& _v, int _i){
 | 
			
		||||
	if(_i == 2 || _i == 5 || _i ==8 ) return;
 | 
			
		||||
	swap(_v[_i], _v[_i + 1]);
 | 
			
		||||
}
 | 
			
		||||
//pos -> <20><EFBFBD>λ<EFBFBD><CEBB> 
 | 
			
		||||
int bfs(vector<int> _aim, vector<int> _v){
 | 
			
		||||
    deque<pair<vector<int>, int>> q;
 | 
			
		||||
    q.push_back({_v, 0});
 | 
			
		||||
    visited.insert(_v);
 | 
			
		||||
 | 
			
		||||
    while(!q.empty()){
 | 
			
		||||
    	auto tmp = q.front(); q.pop_front();
 | 
			
		||||
        vector<int> 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;
 | 
			
		||||
			}
 | 
			
		||||
		} 
 | 
			
		||||
    	// <20><>
 | 
			
		||||
        if(x >= 3){
 | 
			
		||||
            vector<int> tmps = tmpv;
 | 
			
		||||
            swapUp(tmps, x);
 | 
			
		||||
            if(!visited.count(tmps)){
 | 
			
		||||
                visited.insert(tmps);
 | 
			
		||||
                q.push_back({tmps, step + 1});
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // <20><>
 | 
			
		||||
        if(x <= 5){
 | 
			
		||||
            vector<int> tmps = tmpv;
 | 
			
		||||
            swapDown(tmps, x);
 | 
			
		||||
            if(!visited.count(tmps)){
 | 
			
		||||
                visited.insert(tmps);
 | 
			
		||||
                q.push_back({tmps, step + 1});
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // <20><>
 | 
			
		||||
        if(x % 3 != 0){
 | 
			
		||||
            vector<int> tmps = tmpv;
 | 
			
		||||
            swapLeft(tmps, x);
 | 
			
		||||
            if(!visited.count(tmps)){
 | 
			
		||||
                visited.insert(tmps);
 | 
			
		||||
                q.push_back({tmps, step + 1});
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // <20><>
 | 
			
		||||
        if(x % 3 != 2){
 | 
			
		||||
            vector<int> tmps = tmpv;
 | 
			
		||||
            swapRight(tmps, x);
 | 
			
		||||
            if(!visited.count(tmps)){
 | 
			
		||||
                visited.insert(tmps);
 | 
			
		||||
                q.push_back({tmps, step + 1});
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return -1; // <20><EFBFBD>
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(){
 | 
			
		||||
	int n;
 | 
			
		||||
	cin >> n;
 | 
			
		||||
	vector<int> arr(n, -1);
 | 
			
		||||
	vector<int> 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
 | 
			
		||||
*/
 | 
			
		||||
							
								
								
									
										83
									
								
								Algorithm/Enumerate/HuaRongDao-or-Eight-Number-DFS-NO.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								Algorithm/Enumerate/HuaRongDao-or-Eight-Number-DFS-NO.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,83 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <set>
 | 
			
		||||
using namespace std;
 | 
			
		||||
set<vector<int>> visited;
 | 
			
		||||
//<2F><><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϾŹ<CFBE><C5B9><EFBFBD> 
 | 
			
		||||
void swapUp(vector<int>& _v, int _i){
 | 
			
		||||
	if(_i == 0 || _i == 1 || _i ==2 ) return;
 | 
			
		||||
	swap(_v[_i], _v[_i - 3]);
 | 
			
		||||
}
 | 
			
		||||
void swapDown(vector<int>& _v, int _i){
 | 
			
		||||
	if(_i == 6 || _i == 7 || _i ==8 ) return;
 | 
			
		||||
	swap(_v[_i], _v[_i + 3]);
 | 
			
		||||
}
 | 
			
		||||
void swapLeft(vector<int>& _v, int _i){
 | 
			
		||||
	if(_i == 0 || _i == 3 || _i ==6 ) return;
 | 
			
		||||
	swap(_v[_i], _v[_i - 1]);
 | 
			
		||||
}
 | 
			
		||||
void swapRight(vector<int>& _v, int _i){
 | 
			
		||||
	if(_i == 2 || _i == 5 || _i ==8 ) return;
 | 
			
		||||
	swap(_v[_i], _v[_i + 1]);
 | 
			
		||||
}
 | 
			
		||||
//x -> <20><EFBFBD>λ<EFBFBD><CEBB> 
 | 
			
		||||
void dfs(vector<int> _aim, vector<int> _v, int step, int x){
 | 
			
		||||
	if(visited.count(_v)) return; // <20><><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>״̬
 | 
			
		||||
    visited.insert(_v);
 | 
			
		||||
     // <20><>
 | 
			
		||||
    if(x >= 3){
 | 
			
		||||
        vector<int> tmp = _v;
 | 
			
		||||
        swapUp(tmp, x);
 | 
			
		||||
        dfs(_aim, tmp, step + 1, x - 3);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // <20><>
 | 
			
		||||
    if(x <= 5){
 | 
			
		||||
        vector<int> tmp = _v;
 | 
			
		||||
        swapDown(tmp, x);
 | 
			
		||||
        dfs(_aim, tmp, step + 1, x + 3);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // <20><>
 | 
			
		||||
    if(x % 3 != 0){
 | 
			
		||||
        vector<int> tmp = _v;
 | 
			
		||||
        swapLeft(tmp, x);
 | 
			
		||||
        dfs(_aim, tmp, step + 1, x - 1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // <20><>
 | 
			
		||||
    if(x % 3 != 2){
 | 
			
		||||
        vector<int> tmp = _v;
 | 
			
		||||
        swapRight(tmp, x);
 | 
			
		||||
        dfs(_aim, tmp, step + 1, x + 1);
 | 
			
		||||
    }
 | 
			
		||||
    //<2F>Ƚ<EFBFBD> 
 | 
			
		||||
    if(_aim == _v){
 | 
			
		||||
    	cout << step << " ";
 | 
			
		||||
    	return;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(){
 | 
			
		||||
	int n;
 | 
			
		||||
	cin >> n;
 | 
			
		||||
	vector<int> arr(n, -1);
 | 
			
		||||
	vector<int> 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
 | 
			
		||||
*/
 | 
			
		||||
							
								
								
									
										30
									
								
								Algorithm/Enumerate/all_permutation_dfs.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								Algorithm/Enumerate/all_permutation_dfs.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
template <class T>
 | 
			
		||||
void dfs(vector<T>& _path, vector<bool>& _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<int> path;
 | 
			
		||||
	vector<bool> used(n, false);
 | 
			
		||||
	dfs(path, used, n); 
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										25
									
								
								Algorithm/Enumerate/all_permutation_dfs_all_used.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								Algorithm/Enumerate/all_permutation_dfs_all_used.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
template <class T>
 | 
			
		||||
void dfs(vector<T>& _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<int> path;
 | 
			
		||||
	dfs(path, n); 
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										32
									
								
								Algorithm/Enumerate/all_subset.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								Algorithm/Enumerate/all_subset.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
template <class T>
 | 
			
		||||
void dfs(vector<T>& _path, vector<bool>& _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<int> path;
 | 
			
		||||
	vector<bool> used(n, false);
 | 
			
		||||
	dfs(path, used, n); 
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										21
									
								
								Algorithm/Simulate/P9748小苹果.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								Algorithm/Simulate/P9748小苹果.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
using namespace std;
 | 
			
		||||
int main(){
 | 
			
		||||
    int n;
 | 
			
		||||
    cin >> n;
 | 
			
		||||
    vector<int> 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;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user