docs

你正在阅读 v0.1.2 的文档。Nabi 0.1.2 已不再提供支持。 最新版本:v0.1.3 →

本页尚未翻译,以下为英文原文。

#Sudoku solver

本页内容

Solve a sudoku with backtracking over a fixed-size array board, trying each candidate digit in every empty cell.

#Code

#Walkthrough

  • The board is a fixed-size array [[u8; 9]; 9]. The size is part of the type, so cells[r][c] reads and writes directly with no separate allocation (Values and types).
  • solve fixes the board in place through ref self and bumps the caller's counter through ref steps. The caller writes ref too, board.solve(ref steps), so it is visible that the value changes (4.7 The ref keyword, Functions).
  • The heart of backtracking is three lines: place a digit (self.cells[r][c] = d), recursively try to solve the rest, and clear the cell again on failure. If all of 1-9 fail in the first empty cell, false returns and the level above tries another digit.
  • A literal range such as 0..9 takes its type from how the loop variable is used: r and c index the board and are passed as usize arguments, so they are usize with no suffix (1.6 Numeric literals).
  • The puzzle string reader takes only digits and . and skips spaces and newlines, so the board can be laid out readably (Strings).

Found a mistake? Edit this page on GitHub. Last updated