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
本文介绍如何使用回溯法解决八皇后问题,并通过数组替代矩阵存储方式优化内存使用。详细步骤包括初始化数组、回溯算法实现以及输出解决方案。

被折叠的 条评论
为什么被折叠?



