@algorithm.ts/dlx

中文文档
4.0.4发布于 2026/03/23MITTypeScriptESM + CJS零依赖近 30 天下载 1.3万

舞蹈链接 + 算法 X

中文使用文档
文档目录3 节
  1. 安装
  2. 使用量
  3. 相关

@algorithm.ts/dlx (英语)


类型标记执行 DLX 函数 算法.

DLX是应用起舞链接的算法X. 该算法用于解析精确的覆盖 问题.

如果你对这个算法好奇,你可以去拜访 这里 更多细节.

安装

  • 无

    npm install --save @algorithm.ts/dlx
    
  • 小时

    pnpm add @algorithm.ts/dlx
    

使用量

  • 使用 dlx 解决一个 9x9 sudoku 问题 :

    import { DancingLinkX } from '@algorithm.ts/dlx'
    
    const ebs = 1e-6
    const DL_TOTAL_COLUMNS = 9 * 9 * 4
    const DL_MAX_NODES = DL_TOTAL_COLUMNS * (9 * 9 * 9) + 10
    const dlx = new DancingLinkX({ MAX_N: DL_MAX_NODES })
    
    // Sudoku constraints.
    export enum SudokuConstraint {
      SLOT = 0, // Slot(a, b): There must be a number on the grid in row a column b
      ROW = 1, // Row(a, b): Row a must have the number b
      COL = 2, // Col(a, b): Column a must have the number b
      SUB = 3, // Sub(a, b): a-th sub-square matrix must have the number b
    }
    
    export function solveSudoku(puzzle: ReadonlyArray<number[]>, solution: number[][]): boolean {
      const encode = (a: number, b: number, c: number): number => a * 81 + b * 9 + c + 1
    
      dlx.init(DL_TOTAL_COLUMNS)
      const columns: number[] = new Array<number>(4)
      for (let r = 0; r < 9; ++r) {
        for (let c = 0; c < 9; ++c) {
          const w = puzzle[r][c]
          const s = Math.floor(r / 3 + ebs) * 3 + Math.floor(c / 3 + ebs)
          for (let v = 0; v < 9; ++v) {
            if (w === -1 || w === v) {
              columns[0] = encode(SudokuConstraint.SLOT, r, c)
              columns[1] = encode(SudokuConstraint.ROW, r, v)
              columns[2] = encode(SudokuConstraint.COL, c, v)
              columns[3] = encode(SudokuConstraint.SUB, s, v)
              dlx.addRow(encode(r, c, v), columns)
            }
          }
        }
      }
    
      const answer: number[] | null = dlx.solve()
      if (answer === null) return false
    
      for (const _code of answer) {
        let code = _code - 1
        const c = code % 9
    
        code = Math.floor(code / 9 + ebs)
        const b = code % 9
    
        code = Math.floor(code / 9 + ebs)
        const a = code
    
        // eslint-disable-next-line no-param-reassign
        solution[a][b] = c
      }
      return true
    }
    
    const solution = new Array(9)
    for (let r = 0; r < 9; ++r) solution[r] = new Array(9)
    solveSudoku(
      [
        [8, 6, -1, -1, -1, -1, -1, -1, -1],
        [-1, 5, 1, -1, 0, 8, -1, 4, 3],
        [-1, -1, -1, 5, 7, 1, -1, -1, -1],
        [4, 8, 7, 0, -1, -1, 5, -1, 2],
        [-1, -1, -1, 7, 8, 5, -1, 3, -1],
        [0, 3, -1, 2, -1, -1, 8, -1, -1],
        [1, -1, 3, 8, 2, 7, -1, 6, -1],
        [-1, -1, 6, -1, -1, -1, 3, 8, 7],
        [5, 7, -1, 4, -1, 6, 1, -1, -1]
      ],
      solution
    )   // => true
    
    // solution:
    // [
    //   [8, 6, 0, 3, 4, 2, 7, 5, 1],
    //   [7, 5, 1, 6, 0, 8, 2, 4, 3],
    //   [3, 2, 4, 5, 7, 1, 6, 0, 8],
    //   [4, 8, 7, 0, 6, 3, 5, 1, 2],
    //   [6, 1, 2, 7, 8, 5, 0, 3, 4],
    //   [0, 3, 5, 2, 1, 4, 8, 7, 6],
    //   [1, 0, 3, 8, 2, 7, 4, 6, 5],
    //   [2, 4, 6, 1, 5, 0, 3, 8, 7],
    //   [5, 7, 8, 4, 3, 6, 1, 2, 0]
    // ]
    

版本与发布记录

@algorithm.ts/dlx 共发布 60 个版本,首次发布于 2021/07/26;过去 12 个月发布了 1 个版本。

版本发布日期距今
4.0.4latest2026/03/237 个月前
4.0.32024/10/062 年前
4.0.22024/09/142 年前
4.0.12024/05/292 年前
4.0.02024/03/302 年前
3.1.12023/06/103 年前

关于 @algorithm.ts/dlx 的常见问题

@algorithm.ts/dlx 是什么?

舞蹈链接 + 算法 X

如何安装 @algorithm.ts/dlx?

在项目目录运行 npm install @algorithm.ts/dlx,或使用 pnpm add / yarn add / bun add。

@algorithm.ts/dlx 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@algorithm.ts/dlx 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@algorithm.ts/dlx 可以商用吗?

@algorithm.ts/dlx 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@algorithm.ts/dlx 的下载量有多少?

根据 npm 官方下载统计,@algorithm.ts/dlx 在 2026/10/06 – 2026/11/04 这 30 天内被下载 12,662 次,日均约 422 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@algorithm.ts/dlx 还在维护吗?

最新版本 4.0.4 发布于 2026/03/23(7 个月前);至今共发布 60 个版本,其中过去 12 个月发布了 1 个;首次发布于 2021/07/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。