Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
N Qeens problem
//Title of this code #include <iostream> using namespace std; const int n = 3; bool checkQeens(int t[n]) { for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (i != j) { if (t[i] == t[j]) return false; if (t[i] == j && t[j] == i) return false; // one check is missing!!!! } return true; } // 1 2 3 4 // 1 2 3 4 // 1 2 3 4 // 1 2 3 4 // 0 - 1 // 1 - 2 void printQeens(int t[n]) { for (int i = 0; i < n; ++i) cout << t[i] << " "; cout << endl; } // pl: [1,3,2,4,5] int findAllPossibilities(int n) { int t[n]; for (int i = 0; i < n; ++i) t[i] = 0; int sum = 0; int k = 20; while (t[n - 1] != n && k > 0) { printQeens(t); if (checkQeens(t)) ++sum; // increment as an Nth numeral system for (int j = 0; j < n; ++j) { if (t[j] == n - 1) { if (j == n - 1) ++t[j]; else t[j] = 0; } else { ++t[j]; break; } } } return sum; } int main() { std::cout << findAllPossibilities(n); }
run
|
edit
|
history
|
help
0
2
Mine
Camel case
new
PyramidTransitionMatrix_recursive
Tejas choudhari
is_num_palindrom
Shultz_Lab2.CPP
thermal_containers
Christopher-Stellar