Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
N Queens problem
//Title of this code #include <iostream> using namespace std; const int n = 3; bool checkQueens(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 printQueens(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) { printQueens(t); if (checkQueens(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
fibonacci
Bin Tree playground
st_match
break.cpp
LIS
Tray
integer division
reverseKNode
project
Dar