皇后问题 多路递归 回溯

 

线程 栈帧链

 

 

多处理器编程:从入门到放弃 (线程库;现代处理器和宽松内存模型) [南京大学2022操作系统-P3]_哔哩哔哩_bilibili https://www.bilibili.com/video/BV13u411X72Q/?spm_id_from=333.788

 

程序=状态机
 

操作系统上的程序 (什么是程序和编译器) [南京大学2022操作系统-P2]_哔哩哔哩_bilibili https://www.bilibili.com/video/BV12L4y1379V

递归: 函数的一个有一个状态

 

 

多路递归

https://leetcode.cn/problems/combination- dfs/solution/zu-he-zong-he-by-leetcode-solution/

Palindrome Partitioning - LeetCode https://leetcode.com/problems/palindrome-partitioning/

Types of Recursions - GeeksforGeeks https://www.geeksforgeeks.org/types-of-recursions/

递归分类
 
 
 

bug

复制代码
import (
    "log"
    "strings"
)

func solveNQueens(n int) [][]string {
    ans := [][]string{}
    currentPoints := [][]int{}
    ok := func(x, y int) bool {
        m := len(currentPoints)
        for i := 0; i < m; i++ {
            p := currentPoints[i]
            r, c := p[0], p[1]
            if x == r || y == c || x-r == y-c || x-r == c-y {
                return false
            }
        }
        return true
    }
    oneAns := func() []string {
        one := []string{}
        for _, v := range currentPoints {
            a := []string{}
            c := v[1]
            for y := 0; y < n; y++ {
                s := "."
                if y == c {
                    s = "Q"
                }
                a = append(a, s)
            }
            one = append(one, strings.Join(a, ""))
        }
        return one
    }
    var backtrack func(x int)
    backtrack = func(x int) {
        for y := 0; y < n; y++ {
            if x == 0 {
                currentPoints = [][]int{}
            }
            log.Print(x, " ", y, " ", currentPoints)
            if ok(x, y) {
                currentPoints = append(currentPoints, []int{x, y})
                log.Print("OK", currentPoints)
                if len(currentPoints) == n {
                    log.Print("OK-n", n, currentPoints)
                    ans = append(ans, oneAns())
                    return
                }
                backtrack(x + 1)
                currentPoints = currentPoints[0 : len(currentPoints)-1]
            }
        }
    }
    backtrack(0)
    log.Print(ans)
    return ans
}

/*
51. N 皇后 - 力扣(LeetCode) https://leetcode.cn/problems/n-queens/

*/
复制代码

 

复制代码
import (
    "reflect"
    "testing"
)

func Test_solveNQueens(t *testing.T) {
    type args struct {
        n int
    }
    tests := []struct {
        name string
        args args
        want [][]string
    }{
        // TODO: Add test cases.
        // {name: "1", args: args{n: 1}, want: [][]string{{"Q"}}},
        // {name: "4", args: args{n: 4}, want: [][]string{{".Q..", "...Q", "Q...", "..Q."}, {"..Q.", "Q...", "...Q", ".Q.."}}},
        {name: "5", args: args{n: 5}, want: [][]string{{"Q....", "..Q..", "....Q", ".Q...", "...Q."}, {"Q....", "...Q.", ".Q...", "....Q", "..Q.."}, {".Q...", "...Q.", "Q....", "..Q..", "....Q"}, {".Q...", "....Q", "..Q..", "Q....", "...Q."}, {"..Q..", "Q....", "...Q.", ".Q...", "....Q"}, {"..Q..", "....Q", ".Q...", "...Q.", "Q...."}, {"...Q.", "Q....", "..Q..", "....Q", ".Q..."}, {"...Q.", ".Q...", "....Q", "..Q..", "Q...."}, {"....Q", ".Q...", "...Q.", "Q....", "..Q.."}, {"....Q", "..Q..", "Q....", "...Q.", ".Q..."}}},
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := solveNQueens(tt.args.n); !reflect.DeepEqual(got, tt.want) {
                t.Errorf("solveNQueens() = %v, want %v", got, tt.want)
            }
        })
    }
}
复制代码

 

少一行代码

 

posted @   papering  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2021-08-13 文件差异性 订单列表差异性 列表数据更新 比较
2021-08-13 Writing a Time Series Database from Scratch | Fabian Reinartz https://fabxc.org/tsdb/
2021-08-13 Prometheus is a system to collect and process metrics, not an event logging system
2021-08-13 whisper
2021-08-13 New in Prometheus v2.19.0: Memory-mapping of full chunks of the head block reduces memory usage by as much as 40%
2021-08-13 使用proxy是实现集中式和分布式监控的最简单方法
2020-08-13 汽车之家电商平台秒杀系统架构实现
点击右上角即可分享
微信分享提示