zoukankan      html  css  js  c++  java
  • n皇后问题

    n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

    对角线,行,列

    给定一个整数 n,返回 n 皇后不同的解决方案的数量。

    示例:

    输入: 4
    输出: 2
    解释: 4 皇后问题存在如下两个不同的解法。
    [
     [".Q..",  // 解法 1
      "...Q",
      "Q...",
      "..Q."],

     ["..Q.",  // 解法 2
      "Q...",
      "...Q",
      ".Q.."]
    ]

    var demo = function(num){
        let type = 0;
        let col = [];
        //初始化
        let temp  = new Array();
        for(let i=0;i<=num+1;i++){
            temp[i] = new Array();
            for(let j=0;j<=num+1;j++){
                temp[i][j] = 0;
            }
            col[i] = 0;//
        
        }
    
        var check = function(temp,sum,i){
            if(sum === num){
                type ++;
                let t = '';
                for(let i=1;i<=num;i++){
                    for(let j=1;j<=num;j++){
                        t = t+temp[i][j]+" ";
                    }
                    t = t + '
    ';
                }
                console.log(t)
                console.log("=======================================")
                return;
            }
            if(i>num){
                return;
            }
    
            for(let j=1;j<=num;j++){
              
    
                if(col[j]===0&&temp[i-1][j-1]===0&&temp[i-1][j+1]===0){//去除列上相同的,以及最近对角线上的数据
                    let flag = 0;
                    for(let k =1;k<=num;k++){
                        for(let d= 1;d<=num;d++){
                            if(temp[k][d]==1&&(Math.abs(k-i)==Math.abs(d-j))){//检测是否斜对角线上存在。
                                flag= 1;
                                break;
                            }
                        }
                    }
                    if(flag === 0){
                        temp[i][j] = 1;
                        sum = sum + temp[i][j];
                        col[j] = 1;
                       
                        check(temp,sum,i+1);
                        sum = sum - temp[i][j];
                        temp[i][j] = 0;
                        col[j] = 0;
    
                    }
                    
                }
            }
    
    
        }
        check(temp,0,1)
        return type;
    }

    经过多次的回溯联系基本上回溯算法的大概就是

    var demo = function(num){
    
        let type = 0;//初始化一些全局要使用的数组或数据
        var check = function(temp,sum,i){
            if(sum === num){//符合条件返回
                //1.将符合条件的数组或是数据存放到相对应的列表
                //2.将符合条件的个数加一
                return;
            }
            //不符合条件的进行返回
            if(i>num){
                return;
            }
    
            for(let j=1;j<=num;j++){
              
                //将不符合或则使重复的数据过滤掉,剪枝
                if(col[j]===0&&temp[i-1][j-1]===0&&temp[i-1][j+1]===0){
                    //1.提取所有元素都要做的操作
                    push()//将符合条件的元素添加到列表或则是将符合条件的元素
                    check(temp,sum,i+1);//2.将向下进行分支元素的操作
                    pop()//进行回溯,将分支向上回溯
    
                }
            }
    
    
        }
        check(temp,0,1)
        return type;
    }

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/n-queens-ii
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 相关阅读:
    微信小程序 单选按钮 最佳
    微信小程序 单选按钮的实现
    微信小程序 单选框实现
    Java Code To Create Pyramid and Pattern
    Java language
    npm Err! Unexpected end of JSON input while parsing near
    Node.js Express FrameWork Tutorial
    Higher-Order Function Examples
    Create First HTTP Web Server in Node.js: Complete Tutorial
    Node.js NPM Tutorial: Create, Publish, Extend & Manage
  • 原文地址:https://www.cnblogs.com/panjingshuang/p/11668485.html
Copyright © 2011-2022 走看看