八皇后回溯算法C++实现

本文详细介绍了如何使用C++编程语言,通过回溯法解决经典的八皇后问题。内容涵盖了回溯算法的基本原理及其在八皇后问题中的应用,包括棋盘状态的表示、冲突检测和解的输出等关键步骤。

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

#include <iostream>
#include <cstring>
#define QUEENNUM 8//皇后数量(宏)。【宏后面不能加分号】

using namespace std;

int c[QUEENNUM];//各行存放皇后的列的索引的一维数组
int solution = 0;//记录解决方案的数量(全局变量)。记得待执行递归时初始化为0;

/***********************************
功能描述:打印每种方案。
参数:各行存放皇后的索引的一维数组。
***********************************/
void PrintRow(const int*);

/***********************************
功能描述:当前行中放置皇后是否可行。
参数:当前待安排皇后的行的索引
***********************************/
bool IsCurrentRowQueenPosOK(int currentRowIndex);

/**********************************
功能描述:从索引为rowIndex的行开始,递归安排各行皇后的位置
参数:待安排皇后的行的索引
**********************************/
void Queen(int rowIndex);

int main()
{
    solution = 0;
    Queen(0);
    return 0;
}

void PrintRow(const int* everyRowQueenIndexArray)
{
    for(int row = 0; row < QUEENNUM; row++)
    {
        int currentRowQueenIndex = *(everyRowQueenIndexArray + row);
        for(int col = 0; col < QUEENNUM; col++)//当前行各列中,如果列的索引等于当前行中存放皇后的索引,则输出1,否则输出*
        {
            if(col == currentRowQueenIndex)
            {
                cout << "O" << "\t";
            }
            else
            {
                cout << "*" << "\t";
            }
        }
        cout << endl;
    }
}

bool IsCurrentRowQueenPosOK(int currentRowIndex)
{
    //当前行的前面行放置皇后当前情况。当前行的皇后位置满足:
    //与前面各行中皇后不再一列或不在45度角位置
    for(int parentRowIndex = 0; parentRowIndex != currentRowIndex; parentRowIndex++)
    {
        int currentRowPreArrangeQueenPos = c[currentRowIndex];//预安排皇后在当前行中的索引
        int parentEveryRowArrangeQueenPos = c[parentRowIndex];//当前行前几行各行已经安排的皇后的索引
        //以下if语句三种情况不符合:1.与前几行已经存放皇后的各皇后所出列索引一样,即处于同一列;
        //2.位于已安排皇后的左侧45度角位置;3.位于已安排皇后的右侧45度角位置
        if(currentRowPreArrangeQueenPos == parentEveryRowArrangeQueenPos ||
           currentRowIndex - parentRowIndex == -(currentRowPreArrangeQueenPos - parentEveryRowArrangeQueenPos)
           || currentRowIndex - parentRowIndex == currentRowPreArrangeQueenPos - parentEveryRowArrangeQueenPos)
        {
            return false;
        }
    }
    return true;
}

void Queen(int rowIndex)
{
    //此时,当前递归已经把所有行都正确的安排了皇后
    if(rowIndex == QUEENNUM)
    {
        solution++;
        PrintRow(c);
        cout << "***************************************" << endl;
        //cout << "以上为第" + solution +"种方案!" << endl;//这种写法不符合语法要求
        cout << "以上为第" << solution << "种方案!" << endl;
        cout << "***************************************" << endl;
        //cout << "输出下一方案:";
        //cin.get();
    }
    else
    {
        for(int colIndex = 0; colIndex < QUEENNUM; colIndex++)
        {
            c[rowIndex] = colIndex;
            if(IsCurrentRowQueenPosOK(rowIndex))
            {
                Queen(rowIndex + 1);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值