当前位置: 首页 > article >正文

⭐算法OJ⭐N-皇后问题 II【回溯剪枝】(C++实现)N-Queens II

⭐算法OJ⭐N-皇后问题【回溯剪枝】(C++实现)N-Queens

问题描述

The n-queens puzzle is the problem of placing n n n queens on an n × n n \times n n×n chessboard such that no two queens attack each other.

Given an integer n, return the number of distinct solutions to the n-queens puzzle.

Example 1:
在这里插入图片描述

Input: n = 4
Output: 2
Explanation: There are two distinct solutions to the 4-queens puzzle as shown.

Example 2:

Input: n = 1
Output: 1
#include <iostream>
#include <vector>

using namespace std;

// 检查当前位置 (row, col) 是否可以放置皇后
bool isSafe(int row, int col, vector<int>& position) {
    for (int i = 0; i < row; i++) {
        // 检查列和对角线
        if (position[i] == col || abs(position[i] - col) == abs(i - row)) {
            return false;
        }
    }
    return true;
}

// 回溯函数
void solve(int row, vector<int>& position, int n, int& count) {
    // 如果当前行是最后一行,说明找到一个解
    if (row == n) {
        count++; // 解的数量加 1
        return;
    }

    // 尝试在当前行的每一列放置皇后
    for (int col = 0; col < n; col++) {
        if (isSafe(row, col, position)) { // 如果当前位置安全
            position[row] = col; // 记录当前行的皇后位置
            solve(row + 1, position, n, count); // 递归到下一行
            position[row] = -1; // 回溯,撤销皇后
        }
    }
}

// 主函数:求解 N-皇后问题的解的数量
int totalNQueens(int n) {
    int count = 0; // 记录解的数量
    vector<int> position(n, -1); // 记录每一行皇后的列位置,初始为 -1
    solve(0, position, n, count); // 从第 0 行开始回溯
    return count;
}

代码说明

  • isSafe 函数:
    • 检查当前位置 (row, col) 是否可以放置皇后。
    • 检查列和对角线是否有冲突。
  • solve 函数:
    • 使用回溯算法逐行尝试放置皇后。
    • 如果找到一个有效位置,递归到下一行。
    • 如果当前行所有位置都尝试完毕,回溯并撤销上一步的皇后。
  • totalNQueens 函数:
    • 初始化一个数组 position,用于记录每一行皇后的列位置。
    • 调用 solve 函数开始求解,并返回解的数量。

复杂度分析

  • 时间复杂度: O ( N ! ) O(N!) O(N!),因为每行有 N N N 种选择,且需要检查冲突。
  • 空间复杂度: O ( N ) O(N) O(N),用于存储每一行皇后的列位置和递归栈。

http://www.kler.cn/a/577289.html

相关文章:

  • uniapp+<script setup lang=“ts“>使用 uni.$emit和uni.$on全局传递数据
  • Ubuntu系统安装Minikube教程
  • C# Excel开源操作库MiniExcel使用教程
  • 揭开AI-OPS 的神秘面纱 第三讲(下)数据平台层技术架构与组件选型分析
  • 计算机网络——交换机实验(模拟)
  • 大白话html语义化标签优势与应用场景
  • 【Axure高保真原型】多选树筛选表格
  • 【基础知识】回头看Maven基础
  • DeepSeek使用教程--让DeepSeek生成精准题库
  • SQL经典查询
  • DeepSeek R1大语言模型实战工作坊02:deepseek发展演进
  • C++学习(十)(标准,C++11 和 C++14,C++17,C++20)
  • 基于Spring Boot + Vue的图书个性化推荐系统(LW+PPT)
  • Pandas使用笔记
  • 【Go学习实战】03-2-博客查询及登录
  • Docker基础篇——Ubuntu下Docker安装
  • function uuid_generate_v4()不存在(二)
  • Vue3实战学习(IDEA中打开、启动与搭建Vue3工程极简脚手架教程(2025超详细教程)、Windows系统命令行启动Vue3工程)(2)
  • Computational Linguistics期刊全解析:领域顶刊的投稿指南与学术价值
  • 最长递增子序列--蓝桥oj3046拍照