1. Problem Description: Place n queen on a N * N board, and make each two cannot attack each other, that is, each two is not in the same row, the same column and the same bevel line.
2. Design Thought and Analysis: Basic Ideas: X (j) means a space of the solution, J represents the number of rows, the value inside means that the number of columns can be placed, the abstract constraint condition gets the constraints that can be placed (1) X (i)! = X (k); (2) ABS (x (i) -X (k))! = ABS (IK). Applying a backtracking method, when you can put the queen, continue until the next line, return to the first line, re-test the number of columns to put, until all disserted.
#include
/ * Check that you can place a new queen * / bool place (int K, int * x) {INT i; i = 1; while (i
Void main () {cout << "| -------------- N Queen question -------------- |" << endl; cout << "| --- Power by zhanjianTao (028054115) --- |" << endl; cout << "| ------------------------- ------------ | "<< endl << endl; int N; int * x; int i; while (i) {cout <<" Please enter the queen of the queen: "CIN >> N; x = new int [n]; the solution to the COUT << "problem:" << endl; nqueens (n, x); cout << "Press <1> to run again << Endl; cout << "Press <0> to exit" << Endl; CIN >> I;}}