Altered Algorithm
This commit is contained in:
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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||
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
|
||||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
|
||||
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;
|
||||
}
|
Reference in New Issue
Block a user