In order to solve this problem efficiently,
need to use backtracking method instead of iterations. Aka: Backtracking is a general algorithm for finding all (or some) solutions to some computational problem, that incrementally builds candidates to the solutions, and abandons each partial candidate c ("backtracks") as soon as it determines that c cannot possibly be completed to a valid solution. 点击打开WIKI链接
Since all the queens can not be in same column, so use array[SIZE] to store instead of a 8x8 matrix. eg: arr[8]={1,2,3,4,5,6,7} means queen are in diagonal.
one solution can be this:
#include
#include
using namespace std;
const int SIZE=8;
int arr[SIZE];
int ok=0;
void print(){
for(int i=0; i