Ch8.8: 8 queen problem

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章