zoukankan      html  css  js  c++  java
  • N皇后

    递归回溯法

    function putNQueen(n) {
        let res = []; //最终存放结果的数组
        // 核心依赖俩参数 rowIndex,当前想尝试在第几行上放皇后
        // prev 上一次存放皇后的结果,初始值为 [],放的是对应的列值
    
        // 三个数组,已放过的列的值、左斜 rowIndex + columnIndex、右斜对角线线的是否放过 rowIndex - columnIndex
        let hasPutColumnArr = [];
        let leftCorner = [];
        let rightCorner = [];
    
        const record = (rowIndex, columnIndex, bool) => {
            hasPutColumnArr[columnIndex] = bool;
            leftCorner[rowIndex - columnIndex] = bool;
            rightCorner[rowIndex + columnIndex] = bool;
        };
        // 尝试在第n行中摆放皇后的位置
        const putQueen = (rowIndex, prev) => {
            console.log(rowIndex, prev);
            if (rowIndex === n) {
                return res.push(prev);
            }
            for (let columnIndex = 0; columnIndex < n; columnIndex++) {
                const freeColumn = !hasPutColumnArr[columnIndex];
                const freeLeftCorner = !leftCorner[rowIndex + columnIndex];
                const freeRightCorner = !leftCorner[rowIndex - columnIndex];
                // 可放置,则继续递归
                if (freeColumn && freeLeftCorner && freeRightCorner) {
                    // 记录当前行列为已放皇后
                    record(rowIndex, columnIndex, true);
                    // 注意这里prev.concat不会影响出栈后prev的值,prev.concat返回的是拼接后新的值
                    // 注意这里如果用是prev.push,第一传进去的push的item,第二会影响后续prev的值
                    putQueen(rowIndex + 1, prev.concat(columnIndex));
                    // 出栈后将皇后置为未放状态
                    record(rowIndex, columnIndex, false);
                }
            }
        };
        putQueen(0, []);
        return res;
    }
  • 相关阅读:
    UITableViewCell 获取当前位置
    iOS图片拉伸
    TCP/IP基础
    AFNetworking报错"_UTTypeCopyPreferredTagWithClass", referenced from: _AFContentTypeForPathExtens
    iOS 后台处理
    统计iOS项目的总代码行数的方法
    iOS自定义model排序
    iOS开发 适配iOS10
    中文 iOS/Mac 开发博客列表
    C#--静态构造函数
  • 原文地址:https://www.cnblogs.com/hjj2ldq/p/13635313.html
Copyright © 2011-2022 走看看