Run Code
|
API
|
Code Wall
|
Users
|
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
Please
log in
to post a comment.
Project Euler - 113
Heap sort baaaad
namelookup
My love
cast
Test 13(2020)
algortym A*
პირამიდის პერიმეტრი.ფინალური
Stats - Central Limit Theorem - Normal Distribution with multiple items
11
Please log in to post a comment.