Skip to content

Files

Latest commit

aQuaaQua
aQua
and
aQua
Aug 11, 2017
5f61cd3 · Aug 11, 2017

History

History

0037.sudoku-solver

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Aug 10, 2017
Aug 10, 2017
Aug 11, 2017
Aug 11, 2017
Aug 11, 2017

题目

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.

problem

A sudoku puzzle...

solution

...and its solution numbers marked in red.

解题思路

我把数独框中,需要保证数字不重复的3×3小块,称为一个block。 由于数独需要保持每行,每列,每个block中的数字不重复。解题思路如下:

  1. 依次往9个block中,分别填写1~9。
  2. 如果block中已经存在n了,去填写下一个数。
  3. 在可行的空位填好 n 后
    1. 如果后面的填写没有问题,返回true
    2. 如果后面的填写有问题,把n移入下一个可行的位置。
      1. n在这个block中,没有位置放了,返回false
  4. 1~9都填写完了,就去填写下一个block分别填写1~9
  5. 所有的block都填写完了,结束。

具体过程和细节,见程序及注释。

总结

虽然,我一直主张编写不超过20行的函数,但是匿名函数确实好用。