Code:
# Include <iostream> # include <string. h ># include <algorithm> using namespace std; const int N = 100; int c [N]; // The number of columns int n and total in row I of the Queen; int cc; // number of methods void dfs (int cur) {if (cur> n) {cout <"" <cc ++ <"method: "<endl; for (int I = 1; I <= n; I ++) {cout <"no." <I <"" <I <"row no." <c [I] <"column" <<endl ;} total ++; return ;}for (int I = 1; I <= n; I ++) {c [cur] = I; int OK = 1; for (int j = 1; j <cur; j ++) {if (c [cur] = c [j] | Cur-c [cur] = j-c [j] | cur + c [cur] = j + c [j]) {OK = 0; break ;}} if (OK) dfs (cur + 1) ;}} int main () {while (1) {cout <"enter the number of Queens: "<endl; cin> n; if (n <= 0) {cout <" the input is invalid and the program exits! "<Endl; break;} cc = 1; total = 0; dfs (1 ); cout <"total" <total <"solution" <endl;} return 0 ;}
Run:
[Data structure] N Queen problem