• 2021-02-16:n皇后问题。给定一个整数n,返回n皇后的摆法有多少种?


    福哥答案2021-02-16:

    自然智慧即可。
    1.普通递归。有代码。
    需要判断同列和斜线。
    2.位运算递归。有代码。
    3.我的递归。有代码。
    只需要判断斜线。

    代码用golang编写,代码如下:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        n := 12
        fmt.Println(n, "皇后问题")
        fmt.Println("------")
        now := time.Now()
        fmt.Println("1.普通递归:", num1(n))
        fmt.Println("时间:", time.Now().Sub(now))
        fmt.Println("------")
    
        now = time.Now()
        fmt.Println("2.位运算递归:", num2(n))
        fmt.Println("时间:", time.Now().Sub(now))
        fmt.Println("------")
        now = time.Now()
        fmt.Println("3.我的递归:", num3(n))
        fmt.Println("时间:", time.Now().Sub(now))
    }
    func num1(n int) int {
        if n < 1 {
            return 0
        }
        record := make([]int, n)
        return process1(0, record, n)
    }
    func process1(i int, record []int, n int) int {
        if i == n {
            return 1
        }
        res := 0
        for j := 0; j < n; j++ {
            if isValid(record, i, j) {
                record[i] = j
                res += process1(i+1, record, n)
            }
        }
        return res
    }
    func isValid(record []int, i int, j int) bool {
        for k := 0; k < i; k++ {
            if j == record[k] || abs(record[k]-j) == abs(i-k) {
                return false
            }
        }
        return true
    }
    func abs(a int) int {
        if a < 0 {
            return -a
        } else {
            return a
        }
    }
    
    func num2(n int) int {
        if n < 1 || n > 32 {
            return 0
        }
        limit := -1
        if n != 32 {
            limit = (1 << n) - 1
        }
        return process2(limit, 0, 0, 0)
    }
    func process2(limit int, colLim int, leftDiaLim int, rightDiaLim int) int {
        if colLim == limit {
            return 1
        }
        pos := limit & (^(colLim | leftDiaLim | rightDiaLim))
        mostRightOne := 0
        res := 0
        for pos != 0 {
            mostRightOne = pos & (^pos + 1)
            pos = pos - mostRightOne
            res += process2(limit, colLim|mostRightOne, (leftDiaLim|mostRightOne)<<1,
                (rightDiaLim|mostRightOne)>>1)
        }
        return res
    }
    
    func num3(n int) int {
        rest := make([]int, n)
        record := make([]int, n)
        for i := 0; i < n; i++ {
            rest[i] = i
        }
        ansval := 0
        ans := &ansval
        process3(record, 0, rest, ans)
        return *ans
    }
    func process3(record []int, recordLen int, rest []int, ans *int) {
        restLen := len(rest)
        if restLen == 0 {
            *ans++
            return
        }
        for i := 0; i < restLen; i++ {
            isValid := true
            for j := 0; j < recordLen; j++ {
                //不需要看同行和同列,只需要考虑斜线
                if abs(j-recordLen) == abs(record[j]-rest[i]) {
                    isValid = false
                    break
                }
            }
            if isValid {
                record[recordLen] = rest[i]
                restCopy := make([]int, restLen)
                copy(restCopy, rest)
                restCopy = append(restCopy[:i], restCopy[i+1:]...)
                process3(record, recordLen+1, restCopy, ans)
            }
        }
    
    }
    

      

    执行结果如下:

    ***
    [左神java代码](https://github.com/algorithmzuo/algorithmbasic2020/blob/master/src/class23/Code03_NQueens.java)
    [评论](https://user.qzone.qq.com/3182319461/blog/1613432317)

  • 相关阅读:
    MySQL学习-- UNION与UNION ALL
    图解MySQL 内连接、外连接、左连接、右连接、全连接……太多了
    mysql的三种连接方式
    Spring Boot MyBatis配置多种数据库
    @Value注解分类解析
    SpringBoot启动报错Failed to determine a suitable driver class
    idea启动报错:Access denied for user 'root '@'192.168.100.XXX' (using password: YES)
    QStandardItemModel的data线程安全(在插入数据时,临时禁止sizeHint去读model中的data)
    ubuntu 交叉编译qt 5.7 程序到 arm 开发板
    继承QWidget的派生类控件不能设置QSS问题解决(使用style()->drawPrimitive(QStyle::PE_Widget,也就是画一个最简单最原始的QWidget,不要牵扯其它这么多东西)
  • 原文地址:https://www.cnblogs.com/waitmoon/p/14407691.html
Copyright © 2020-2023  润新知