2026.02.01 n皇后 & 解数独

51. N 皇后

python 复制代码
class Solution:
    def solveNQueens(self, n: int) -> List[List[str]]:
        col = [0] * n
        right_left = [0] * 2 * n
        left_right = [0] * 2 * n
        res = []
        path = []

        def backtrack(row : int) -> None:
            if len(path) == n:
                res.append(path[:])
                return
            for i in range(0, n):
                # 每一行从0 -> n 遍历
                if col[i] or right_left[row + i] or left_right[n - row + i - 1]:
                    # 如果冲突则跳过当前位置
                    continue
                # 构建当前行的放置
                path_str = ""
                for j in range(0, n):
                    if j == i:
                        path_str += 'Q'
                    else:
                        path_str += '.'
                
                path.append(path_str)
                col[i] = 1
                right_left[row + i] = 1
                left_right[n - row + i - 1] = 1
                backtrack(row + 1)
                path.pop()
                col[i] = 0
                right_left[row + i] = 0
                left_right[n - row + i - 1] = 0
        backtrack(0)
        return res
                

37. 解数独

python 复制代码
class Solution:
    def solveSudoku(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        # 记录行、列、3 * 3 box是否出现过某个数字, row[r][num]代表第r行是否存在数字num
        row = [[False] * 10 for _ in range(9)]
        col = [[False] * 10 for _ in range(9)]
        box = [[[False] * 10 for _ in range(3)] for _ in range(3)]

        # 存储所有空白位置
        spaces = []
        # 初始化空间
        for r in range(9):
            for c in range(9):
                if board[r][c] == '.':
                    spaces.append((r, c))
                else:
                    num = int(board[r][c])
                    row[r][num] = True
                    col[c][num] = True
                    box[r // 3][c // 3][num] = True
        def backtrack(index : int) -> bool:
            if index == len(spaces):
                # 如果填完了所有空白就结束返回
                return True
            r, c = spaces[index]
            for num in range(1, 10):
                # 对每个空白格,尝试填入 1-9
                if row[r][num] or col[c][num] or box[r // 3][c // 3][num]:
                    continue
                
                # 做出选择
                board[r][c] = str(num)
                row[r][num] = True
                col[c][num] = True
                box[r // 3][c // 3][num] = True

                if backtrack(index + 1):
                    return True
                
                # 撤销选择
                board[r][c] = "."
                row[r][num] = False
                col[c][num] = False
                box[r // 3][c // 3][num] = False

            return False
        backtrack(0)

            

        
相关推荐
默_笙3 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003963 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技3 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时3 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
奇思妙想聪明勤奋的小羊3 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1233 天前
2026年第38周GitHub趋势周报
python·科技·github