Ch8.8: 8 queen problem

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
    
   
  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值