TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Solving Every Sudoku Puzzle (2006)

80 点作者 srathi将近 10 年前

13 条评论

mdup将近 10 年前
Another way to solve Sudoku is to understand that it is an instance of the Exact Cover problem [1]. Basically, solving a 3x3 Sudoku grid is nothing more than finding 9 &quot;sheets&quot; of each number which superimpose exactly on top of the grid.<p>For example (2x2 sudoku = 4 sheets):<p><pre><code> 1324 3142 4213 2431 </code></pre> is:<p><pre><code> 1... .2.. .3.. ...4 .1.. ...2 3... ..4. ..1. + .2.. + ...3 + 4... ...1 2... ..3. .4.. </code></pre> or even:<p><pre><code> x... .x.. .x.. ...x .x.. ...x x... ..x. ..x. + .x.. + ...x + x... ...x x... ..x. .x.. x... .x.. ..x. ...x &lt;- number row </code></pre> where the last &quot;number row&quot; does not belong to the grid but serves to indicate the number of the sheet.<p>This explains how sudoku relates to the Exact Cover problem. Now, there is a good algorithm by Knuth to solve Exact Cover, which is known as Algorithm X [2].<p>Algorithm X is just an algorithm but Knuth has also invented a very efficient implementation of it, called Dancing Links or DLX for short [3]. DLX is described in a very good and very readable paper by Knuth himself [4]. This is one of the first serious papers I ever read and it was a real joy, mind-opener.<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Exact_cover" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Exact_cover</a><p>[2] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Knuth%27s_Algorithm_X" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Knuth%27s_Algorithm_X</a><p>[3] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Dancing_Links" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Dancing_Links</a><p>[4] <a href="http:&#x2F;&#x2F;www-cs-faculty.stanford.edu&#x2F;~uno&#x2F;papers&#x2F;dancing-color.ps.gz" rel="nofollow">http:&#x2F;&#x2F;www-cs-faculty.stanford.edu&#x2F;~uno&#x2F;papers&#x2F;dancing-color...</a>
评论 #9901813 未加载
justinator将近 10 年前
Sudoku solver using Perl regex <a href="http:&#x2F;&#x2F;www.perlmonks.org&#x2F;?node_id=471168" rel="nofollow">http:&#x2F;&#x2F;www.perlmonks.org&#x2F;?node_id=471168</a><p>Sudoku solver in three lines of Perl (golf): <a href="https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20070106133158&#x2F;http:&#x2F;&#x2F;www.ecclestoad.co.uk&#x2F;blog&#x2F;2005&#x2F;06&#x2F;02&#x2F;sudoku_solver_in_three_lines_explained.html" rel="nofollow">https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20070106133158&#x2F;http:&#x2F;&#x2F;www.eccles...</a><p><pre><code> use integer;@A=split&#x2F;&#x2F;,&lt;&gt;;sub R{for$i(0..80){next if$A[$i];my%t=map{$_&#x2F;9 ==$i&#x2F;9||$_%9==$i%9||$_&#x2F;27==$i&#x2F;27&amp;&amp;$_%9&#x2F;3==$i%9&#x2F;3?$A[$_]:0=&gt;1}0..80;R($A[ $i]=$_)for grep{!$t{$_}}1..9;return$A[$i]=0}die@A}R</code></pre>
wickawic将近 10 年前
I&#x27;ve noticed that solving a sudoku is something of a rosetta stone for how people program. Norvig&#x27;s solution is very straightforward in hindsight, but I think that code was either the result of many iterations or of a career of making progressively more readable code.<p>Here are some other sudoku solvers that give more insight into the coder than the problem:<p>Sudoku in APL: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=DmT80OseAGs" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=DmT80OseAGs</a><p>Test driven sudoku: <a href="http:&#x2F;&#x2F;ronjeffries.com&#x2F;xprog&#x2F;articles&#x2F;sudokumusings&#x2F;" rel="nofollow">http:&#x2F;&#x2F;ronjeffries.com&#x2F;xprog&#x2F;articles&#x2F;sudokumusings&#x2F;</a>
评论 #9901422 未加载
评论 #9902540 未加载
asgard1024将近 10 年前
It has been shown in 2012 that you need at least 17 digits to uniquely specify a Sudoku puzzle: <a href="http:&#x2F;&#x2F;www.math.ie&#x2F;McGuire_V1.pdf" rel="nofollow">http:&#x2F;&#x2F;www.math.ie&#x2F;McGuire_V1.pdf</a><p>Their approach was quite close to <i>solving every sudoku puzzle</i>, although they traversed the space by the solutions.
yuchi将近 10 年前
Small discovery: Brendan Eich implemented generators because he found them incredibly important to translate this code in JS.<p>In fact in the ‘Translations’ section at the end there’s a link to this Bugzilla ticket: <a href="https:&#x2F;&#x2F;bugzilla.mozilla.org&#x2F;show_bug.cgi?id=380237" rel="nofollow">https:&#x2F;&#x2F;bugzilla.mozilla.org&#x2F;show_bug.cgi?id=380237</a>
mythz将近 10 年前
FWIW I&#x27;ve got benchmarks and ports of Norvig&#x27;s sudoku solver in Python&#x2F;PyPy, Dart, C#, Ruby and CoffeeScript at:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;dartist&#x2F;sudoku_solver" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dartist&#x2F;sudoku_solver</a>
mrspeaker将近 10 年前
I love that this code is so elegant, but (and!) chooses strings and string manipulation for storage of the possible cell values.<p>It seems like he started with lists and switched when he got to the search method (&quot;This is why I chose to implement the set of possible values for a square as a string: I can copy values with values.copy() which is simple and efficient.&quot;) - It&#x27;s probably not an amazing insight, but I&#x27;m sure my brain would have been yelling at me &quot;don&#x27;t use strings! don&#x27;t use strings!&quot;.
deepaksurti将近 10 年前
I implemented a solver in Common Lisp, quite a while back. The repo:<a href="https:&#x2F;&#x2F;github.com&#x2F;dmsurti&#x2F;sudoku" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dmsurti&#x2F;sudoku</a> and some details here: <a href="http:&#x2F;&#x2F;www.deepaksurti.com&#x2F;blog&#x2F;my-attempt-to-solve-sudoku" rel="nofollow">http:&#x2F;&#x2F;www.deepaksurti.com&#x2F;blog&#x2F;my-attempt-to-solve-sudoku</a>.<p>I was learning algorithms then and a sudoku solver was both fun and a good learning exercise.
sadgit将近 10 年前
Good time to pimp my c implementation of Norvig&#x27;s algorithm:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;ssadler&#x2F;ssadler-various&#x2F;blob&#x2F;master&#x2F;sudoku-hammer&#x2F;sudoku.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ssadler&#x2F;ssadler-various&#x2F;blob&#x2F;master&#x2F;sudok...</a><p>(This code is completely indulgent but very fast!)
discardorama将近 10 年前
If someone is interested in seeing Sudoku solved in other languages, here&#x27;s a nice page: <a href="http:&#x2F;&#x2F;rosettacode.org&#x2F;wiki&#x2F;Sudoku" rel="nofollow">http:&#x2F;&#x2F;rosettacode.org&#x2F;wiki&#x2F;Sudoku</a><p>Not all of the code is efficient, but it&#x27;s interesting nevertheless.
thret将近 10 年前
I feel like the &#x27;search&#x27; function is missing the point. The sudoku puzzles are generated in such a way that there is always a next move to be deduced: there is never any need to guess. If you have to guess, then you have missed something.
评论 #9903300 未加载
vtsrh将近 10 年前
I find it rather disappointing that it still takes on the order of 10^2 milliseconds to solve the hardest sudokus.<p>This was tested with my own algorithm and one supposedly fast found online. I will revise my code, but I doubt I will improve the speed.
JohnyLy将近 10 年前
This is good to know that we can win Sudoku with a computer program but not surprising. The goal of Sudoku is to make your intellect work and find logic by yourself. The article should be named: How to win every Sudoku puzzle by cheating.
评论 #9901514 未加载
评论 #9901347 未加载
评论 #9901349 未加载
评论 #9901317 未加载