# @algorithm\.ts/dlx 中文文档

规范地址：https://npmcn.com/package/%40algorithm.ts/dlx/

版本：4\.0\.4
npm 元数据更新时间：2026-03-23T11:39:01.180Z
中文译文更新时间：2026-11-05T22:22:42.907Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40algorithm.ts/dlx
原文对照：https://npmcn.com/package/%40algorithm.ts/dlx/?lang=original
项目主页：https://github.com/guanghechen/algorithm.ts/tree/@algorithm.ts/dlx@4.0.3/packages/dlx#readme
源码仓库：https://github.com/guanghechen/algorithm.ts#@algorithm.ts

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：39.1 kB，7 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2021-07-26T08:18:29.515Z
- 最新版本发布：2026-03-23T11:39:01.053Z
- 近 30 天下载量：12662（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：60（近 12 个月 1 个）

## 中文简介

舞蹈链接 + 算法 X

## 安装

```sh
npm install @algorithm.ts/dlx
```

也可以使用：`pnpm add @algorithm.ts/dlx`、`yarn add @algorithm.ts/dlx`、`bun add @algorithm.ts/dlx`

## README

# [@algorithm.ts/dlx (英语)](https://github.com/guanghechen/algorithm.ts/tree/@algorithm.ts/dlx@4.0.3/packages/dlx#readme)

 [![Npm Version](https://img.shields.io/npm/v/@algorithm.ts/dlx.svg)](https://www.npmjs.com/package/@algorithm.ts/dlx)[![Npm Download](https://img.shields.io/npm/dm/@algorithm.ts/dlx.svg) ](https://www.npmjs.com/package/@algorithm.ts/dlx)[![Npm License](https://img.shields.io/npm/l/@algorithm.ts/dlx.svg) ](https://www.npmjs.com/package/@algorithm.ts/dlx)[![Module Formats: cjs, esm](https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg) ](https://npmcn.com/package/%40algorithm.ts/dlx/#readme-install)[![Node.js Version](https://img.shields.io/node/v/@algorithm.ts/dlx) ](https://github.com/nodejs/node)[![Tested with Jest](https://img.shields.io/badge/tested_with-vitest-6E9F18.svg) ](https://github.com/vitest-dev/vitest)[![Code Style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)

  

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

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

如果你对这个算法好奇,你可以去拜访 [这里](https://me.guanghechen.com/post/algorithm/shuffle/#heading-dlx) 更多细节.

## 安装

-   无
    
    ```bash
    npm install --save @algorithm.ts/dlx
    ```
    
-   小时
    
    ```bash
    pnpm add @algorithm.ts/dlx
    ```
    

## 使用量

-   使用 dlx 解决一个 9x9 sudoku 问题 :
    
    ```typescript
    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]
    // ]
    ```
    

## 相关

-   [时间轴:](https://me.guanghechen.com/post/algorithm/shuffle/#heading-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 和提交记录判断。

