Python3でArto Inkala氏の「世界一難しい数独」を解きます
世界一速いPeter Norvig氏のPython3プログラムが実際に動作します
「世界一難しい数独」の難易度は,25,31,36(2006, 2010, 2012年版)
Python3(3.10)で動くソースコード(.pyファイル .ipynbファイル)あります
「anaconda3」on .py「PyCharm」.ipynb「Jupyter Notebook」
数独の難易度とは,絞り込みの結果残る候補の個数を掛け合わせた数字;残りの探索回数の指数部
そこでPython3.6に改造して問題の解答を表示できるようにして,さらに,問題をランダムに作ることができるように改造しました。
問題の解答を表示するプログラム,問題をランダムにつくるプログラムは左上隅からダウンロードできます。プログラムの動作はこのWebページで解説します。プログラムの使い方はこのWebサイトの別記事で解説します。
https://yamakatsusan.web.fc2.com/python16.html
『Arto Inkala氏の「世界一難しい数独」より難しい数独問題を無制限につくる』
ここでは,オリジナルプログラムをPython3.6に改造して,その動作を巧みなデバッグで解説します。Python3プログラムは巻末に掲載してあり,左上隅からダウンロードできます。ダウンロードファイルの解説は巻末にあります。
(2023-11-14)Python3.10で動作確認済み,コードを追加「time.clock = time.time」,旧いモジュールが使えなくなったので新しいモジュールを変換して旧いプログラムと互換とする。
「Pycharm」では,def test()が存在するだけでエラーになるのでコメントアウトしました。「Jupyter Notebook」では,正常に動作します。巻末のソースコードと左上隅からダウンロードできる「.py」ファイルは修正してあります(動作確認済)。
(2023-12-12)「世界一難しい数独」(2006, 2010, 2012年版)とプログラムにハードコードされている「hard1」の解答時間を測定しました(「hard1」を9x9に書き直したものは本記事と関連別記事の中にあります)。
解答時間は,
0.004, 0.006, 0.030, 52.950 秒
難易度は,(意味はあとで説明します)
25,31,36, 46(これが本当の「世界一難しい数独」かも。人では解けないおそれがあります)
左上隅からダウンロードできる「sudoku3X_txt.py」を使用。
自己テストの 203, 207行を run すると問題,解答,動作時間が表示されます。表示桁数は 162行の書式を替えます。難易度は「sudoku3X_parse.py」で表示されます(関連別記事参照)。
次図の左は2010年版,右は2012年版です。2006版も含めて,問題と解答は次図の下の表にあります。数独の難易度が解説が下にあります。理論的に納得できる定義です。
8 5 . |. . 2 |4 . . 7 2 . |. . . |. . 9 . . 4 |. . . |. . . ------+------+------ . . . |1 . 7 |. . 2 3 . 5 |. . . |9 . . . 4 . |. . . |. . . ------+------+------ . . . |. 8 . |. 7 . . 1 7 |. . . |. . . . . . |. 3 6 |. 4 . squares = 22 level = 1.7e+25 8 5 1369 | 36 1679 2 | 4 36 1367 7 2 136 | 34568 156 345 | 13568 3568 9 169 369 4 | 3568 15679 359 | 135678 2 135678 ---------------------+---------------------+--------------------- 69 689 689 | 1 4 7 | 3568 3568 2 3 7 5 | 26 26 8 | 9 1 4 1269 4 12689 | 2356 2569 359 | 35678 3568 35678 ---------------------+---------------------+--------------------- 4 36 236 | 9 8 1 | 2356 7 356 256 1 7 | 245 25 45 | 23568 9 3568 259 89 289 | 7 3 6 | 1258 4 158 8 5 9 |6 1 2 |4 3 7 7 2 3 |8 5 4 |1 6 9 1 6 4 |3 7 9 |5 2 8 ------+------+------ 9 8 6 |1 4 7 |3 5 2 3 7 5 |2 6 8 |9 1 4 2 4 1 |5 9 3 |7 8 6 ------+------+------ 4 3 2 |9 8 1 |6 7 5 6 1 7 |4 2 5 |8 9 3 5 9 8 |7 3 6 |2 4 1 ======== . . 5 |3 . . |. . . 8 . . |. . . |. 2 . . 7 . |. 1 . |5 . . ------+------+------ 4 . . |. . 5 |3 . . . 1 . |. 7 . |. . 6 . . 3 |2 . . |. 8 . ------+------+------ . 6 . |5 . . |. . 9 . . 4 |. . . |. 3 . . . . |. . 9 |7 . . squares = 23 level = 2.9e+31 1269 249 5 | 3 24689 24678 |14689 14679 1478 8 349 169 | 4679 5 467 | 1469 2 1347 2369 7 269 | 4689 1 2468 | 5 469 348 ------------------+------------------+------------------ 4 289 26789 | 1689 689 5 | 3 179 127 259 1 289 | 489 7 3 | 249 459 6 5679 59 3 | 2 469 146 | 149 8 1457 ------------------+------------------+------------------ 1237 6 1278 | 5 2348 12478 | 1248 14 9 12579 2589 4 | 1678 268 12678 | 1268 3 1258 1235 2358 128 | 1468 23468 9 | 7 1456 12458 1 4 5 |3 2 7 |6 9 8 8 3 9 |6 5 4 |1 2 7 6 7 2 |9 1 8 |5 4 3 ------+------+------ 4 9 6 |1 8 5 |3 7 2 2 1 8 |4 7 3 |9 5 6 7 5 3 |2 9 6 |4 8 1 ------+------+------ 3 6 7 |5 4 2 |8 1 9 9 8 4 |7 6 1 |2 3 5 5 2 1 |8 3 9 |7 6 4 ======== 8 . . |. . . |. . . . . 3 |6 . . |. . . . 7 . |. 9 . |2 . . ------+------+------ . 5 . |. . 7 |. . . . . . |. 4 5 |7 . . . . . |1 . . |. 3 . ------+------+------ . . 1 |. . . |. 6 8 . . 8 |5 . . |. 1 . . 9 . |. . . |4 . . squares = 21 level = 9.6e+36 8 1246 24569 | 2347 12357 1234 | 13569 4579 1345679 12459 124 3 | 6 12578 1248 | 1589 45789 14579 1456 7 456 | 348 9 1348 | 2 458 13456 ------------------------+------------------------+------------------------ 123469 5 2469 | 2389 2368 7 | 1689 2489 12469 12369 12368 269 | 2389 4 5 | 7 289 1269 24679 2468 24679 | 1 268 2689 | 5689 3 24569 ------------------------+------------------------+------------------------ 23457 234 1 | 23479 237 2349 | 359 6 8 23467 2346 8 | 5 2367 23469 | 39 1 2379 23567 9 2567 | 2378 123678 12368 | 4 257 2357 8 1 2 |7 5 3 |6 4 9 9 4 3 |6 8 2 |1 7 5 6 7 5 |4 9 1 |2 8 3 ------+------+------ 1 5 4 |2 3 7 |8 9 6 3 6 9 |8 4 5 |7 2 1 2 8 7 |1 6 9 |5 3 4 ------+------+------ 5 2 1 |9 7 4 |3 6 8 4 3 8 |5 2 6 |9 1 7 7 9 6 |3 1 8 |4 5 2 ======== . . . |. . 6 |. . . . 5 9 |. . . |. . 8 2 . . |. . 8 |. . . ------+------+------ . 4 5 |. . . |. . . . . 3 |. . . |. . . . . 6 |. . 3 |. 5 4 ------+------+------ . . . |3 2 5 |. . 6 . . . |. . . |. . . . . . |. . . |. . . squares = 17 level = 4.4e+46 13478 1378 1478 | 124579 134579 6 | 1234579 123479 123579 13467 5 9 | 1247 1347 1247 | 123467 123467 8 2 1367 147 | 14579 134579 8 | 1345679 134679 13579 ---------------------------+---------------------------+--------------------------- 1789 4 5 | 126789 16789 1279 | 1236789 1236789 12379 1789 12789 3 | 12456789 1456789 12479 | 126789 126789 1279 1789 12789 6 | 12789 1789 3 | 12789 5 4 ---------------------------+---------------------------+--------------------------- 14789 1789 1478 | 3 2 5 | 14789 14789 6 13456789 1236789 12478 | 146789 146789 1479 | 12345789 1234789 123579 13456789 1236789 12478 | 146789 146789 1479 | 12345789 1234789 123579 4 3 8 |7 9 6 |2 1 5 6 5 9 |1 3 2 |4 7 8 2 7 1 |4 5 8 |6 9 3 ------+------+------ 8 4 5 |2 1 9 |3 6 7 7 1 3 |5 6 4 |8 2 9 9 2 6 |8 7 3 |1 5 4 ------+------+------ 1 9 4 |3 2 5 |7 8 6 3 6 2 |9 8 7 |5 4 1 5 8 7 |6 4 1 |9 3 2
上の表はそれらの数独を解いた結果です(添付のソースで)。数独の問題を解くとき最初に機械的な絞り込みをやります。確定するマスといくつかの候補が残るマスがあります。それを問題の後に表示しています。
この候補の個数を掛け合わせた数字は'level'であり,意味は残りの探索回数です。大きな数字なのでその指数部分に注目します。それが難易度なのです。10の何乗とかと言う累乗の部分です。
それが,25,31,36,46 なのです。
'level'の上の'squares'は問題の埋められている数字の個数です。これは難易度と弱い逆相関があります。当然,少ない方が難易度が高いです。
◆◆Peter Norvig氏のPython3プログラムは解も絞り込み結果も表示する◆◆
Peter Norvig氏は,やさしい数独から難しい数独まで150問ほど用意してくれました。問題の書式は何種類かあります。1つだけ1分ほどかかった問題がありましたが難易度が46でした。残りの探索回数が10の46乗ということです。
Arto Inkala氏の世界一難しい数独は中間くらいの難易度でした。2012年版でも難易度は,36です。前述の難易度46は,人間では解けないと思います。
最もやさしい問題は絞り込みの結果で各マスが確定し,候補の個数を掛け合わせた数字は1,難易度は0になります。
Peter Norvig氏は,ランダムに問題を作成するので,それから絞り込みの結果と難易度を表示させると,難易度ごとに分けられます。
問題を解くのはこのWebページ,問題を作成するのはこのWebサイトの別記事を参照してください。左上隅からすべてダウンロードできます。
・外部ファイルの問題を解くソースコードは,sudoku3X_txt.ipynb
・問題をつくりながら解くソースコードは,sudoku3X_parse.ipynb
◆◆Arto Inkala氏の「世界一難しい数独」を解くPeter Norvig氏のPythonプログラム◆◆
世界一難しい数独を世界一速く解くこのプログラムを巧みなデバッグでその動作を詳細に解説します。
数独を解くプログラムは,配列のオバケになりがちです。ですが,Peter Norvig氏のプログラムは,ハッシュ(辞書)を使い,配列の操作がほとんどありません。また,数独は探索回数が膨大になりますが,このアルゴリズムはそれを回避しています。
巧みなデバッグでそれらの秘密をお見せします。
ソースコードと問題は,左上隅からダウンロードできます。ソースコードはこのWebページの巻末に掲載してあります。All tests pass. Solved 50 of 50 easy puzzles (avg 0.01 secs (193 Hz), max 0.01 secs). Solved 95 of 95 hard puzzles (avg 0.02 secs (53 Hz), max 0.09 secs). Solved 11 of 11 hardest puzzles (avg 0.01 secs (141 Hz), max 0.01 secs). Solved 99 of 99 random puzzles (avg 0.01 secs (178 Hz), max 0.01 secs).
198 199 200 201 202 203 204 205 206 207
if __name__ == '__main__': # test() solve_all(from_file("easy50.txt", '========'), "easy", 0.0) solve_all(from_file("top95.txt"), "hard", 0.0) solve_all(from_file("hardest.txt"), "hardest", 0.0) solve_all(from_file("Arto Inkala.txt", '========'), "Arto Inkala", 0.0) solve_all([random_puzzle() for _ in range(99)], "random", 100.0) solve_all([grid1]) # 191行目 solve_all([grid2]) solve_all([hard1]) # 1分ほどかかる
201
solve_all(from_file("hardest.txt")[0:2], 'Inkala') 8 5 . |. . 2 |4 . . 7 2 . |. . . |. . 9 . . 4 |. . . |. . . ------+------+------ . . . |1 . 7 |. . 2 3 . 5 |. . . |9 . . . 4 . |. . . |. . . ------+------+------ . . . |. 8 . |. 7 . . 1 7 |. . . |. . . . . . |. 3 6 |. 4 . 8 5 9 |6 1 2 |4 3 7 7 2 3 |8 5 4 |1 6 9 1 6 4 |3 7 9 |5 2 8 ------+------+------ 9 8 6 |1 4 7 |3 5 2 3 7 5 |2 6 8 |9 1 4 2 4 1 |5 9 3 |7 8 6 ------+------+------ 4 3 2 |9 8 1 |6 7 5 6 1 7 |4 2 5 |8 9 3 5 9 8 |7 3 6 |2 4 1 (0.01 seconds) . . 5 |3 . . |. . . 8 . . |. . . |. 2 . . 7 . |. 1 . |5 . . ------+------+------ 4 . . |. . 5 |3 . . . 1 . |. 7 . |. . 6 . . 3 |2 . . |. 8 . ------+------+------ . 6 . |5 . . |. . 9 . . 4 |. . . |. 3 . . . . |. . 9 |7 . . 1 4 5 |3 2 7 |6 9 8 8 3 9 |6 5 4 |1 2 7 6 7 2 |9 1 8 |5 4 3 ------+------+------ 4 9 6 |1 8 5 |3 7 2 2 1 8 |4 7 3 |9 5 6 7 5 3 |2 9 6 |4 8 1 ------+------+------ 3 6 7 |5 4 2 |8 1 9 9 8 4 |7 6 1 |2 3 5 5 2 1 |8 3 9 |7 6 4 (0.01 seconds)
200 201 202
solve_all([grid1]) solve_all([grid2]) solve_all([hard1]) All tests pass. 0 0 3 |0 2 0 |6 0 0 9 0 0 |3 0 5 |0 0 1 0 0 1 |8 0 6 |4 0 0 ------+------+------ 0 0 8 |1 0 2 |9 0 0 7 0 0 |0 0 0 |0 0 8 0 0 6 |7 0 8 |2 0 0 ------+------+------ 0 0 2 |6 0 9 |5 0 0 8 0 0 |2 0 3 |0 0 9 0 0 5 |0 1 0 |3 0 0 4 8 3 |9 2 1 |6 5 7 9 6 7 |3 4 5 |8 2 1 2 5 1 |8 7 6 |4 9 3 ------+------+------ 5 4 8 |1 3 2 |9 7 6 7 2 9 |5 6 4 |1 3 8 1 3 6 |7 9 8 |2 4 5 ------+------+------ 3 7 2 |6 8 9 |5 1 4 8 1 4 |2 5 3 |7 6 9 6 9 5 |4 1 7 |3 8 2 (0.00 seconds) 4 . . |. . . |8 . 5 . 3 . |. . . |. . . . . . |7 . . |. . . ------+------+------ . 2 . |. . . |. 6 . . . . |. 8 . |4 . . . . . |. 1 . |. . . ------+------+------ . . . |6 . 3 |. 7 . 5 . . |2 . . |. . . 1 . 4 |. . . |. . . 4 1 7 |3 6 9 |8 2 5 6 3 2 |1 5 8 |9 4 7 9 5 8 |7 2 4 |3 1 6 ------+------+------ 8 2 5 |4 3 7 |1 6 9 7 9 1 |5 8 6 |4 3 2 3 4 6 |9 1 2 |7 5 8 ------+------+------ 2 8 9 |6 4 3 |5 7 1 5 7 3 |2 9 1 |6 8 4 1 6 4 |8 7 5 |2 9 3 (0.01 seconds) . . . |. . 6 |. . . . 5 9 |. . . |. . 8 2 . . |. . 8 |. . . ------+------+------ . 4 5 |. . . |. . . . . 3 |. . . |. . . . . 6 |. . 3 |. 5 4 ------+------+------ . . . |3 2 5 |. . 6 . . . |. . . |. . . . . . |. . . |. . . 4 3 8 |7 9 6 |2 1 5 6 5 9 |1 3 2 |4 7 8 2 7 1 |4 5 8 |6 9 3 ------+------+------ 8 4 5 |2 1 9 |3 6 7 7 1 3 |5 6 4 |8 2 9 9 2 6 |8 7 3 |1 5 4 ------+------+------ 1 9 4 |3 2 5 |7 8 6 3 6 2 |9 8 7 |5 4 1 5 8 7 |6 4 1 |9 3 2 (68.47 seconds)
200 201 202
display(solve(grid1)) display(solve(grid2)) display(solve(hard1)) (問題は出力されません)
main | solve_all | time_solve | solve | search | parse_grid | assign | eliminate |
grid_values | |||||||
assign | eliminate | ||||||
display | |||||||
solved | unitsolved | ||||||
from_file | |||||||
random_puzzle | assign | eliminate | |||||
display | solve | search | parse_grid | grid_values | |||
assign | eliminate | ||||||
assign | eliminate | ||||||
test |
A1 A2 A3| A4 A5 A6| A7 A8 A9 4 . . |. . . |8 . 5 4 1 7 |3 6 9 |8 2 5 B1 B2 B3| B4 B5 B6| B7 B8 B9 . 3 . |. . . |. . . 6 3 2 |1 5 8 |9 4 7 C1 C2 C3| C4 C5 C6| C7 C8 C9 . . . |7 . . |. . . 9 5 8 |7 2 4 |3 1 6 ---------+---------+--------- ------+------+------ ------+------+------ D1 D2 D3| D4 D5 D6| D7 D8 D9 . 2 . |. . . |. 6 . 8 2 5 |4 3 7 |1 6 9 E1 E2 E3| E4 E5 E6| E7 E8 E9 . . . |. 8 . |4 . . 7 9 1 |5 8 6 |4 3 2 F1 F2 F3| F4 F5 F6| F7 F8 F9 . . . |. 1 . |. . . 3 4 6 |9 1 2 |7 5 8 ---------+---------+--------- ------+------+------ ------+------+------ G1 G2 G3| G4 G5 G6| G7 G8 G9 . . . |6 . 3 |. 7 . 2 8 9 |6 4 3 |5 7 1 H1 H2 H3| H4 H5 H6| H7 H8 H9 5 . . |2 . . |. . . 5 7 3 |2 9 1 |6 8 4 I1 I2 I3| I4 I5 I6| I7 I8 I9 1 . 4 |. . . |. . . 1 6 4 |8 7 5 |2 9 3 A2 | | | | A1 A2 A3| | B2 | | | | B1 B2 B3| | C2 | | C1 C2 C3| C4 C5 C6| C7 C8 C9 C1 C2 C3| | ---------+---------+--------- ---------+---------+--------- ---------+---------+--------- D2 | | | | | | E2 | | | | | | F2 | | | | | | ---------+---------+--------- ---------+---------+--------- ---------+---------+--------- G2 | | | | | | H2 | | | | | | I2 | | | | | |
200 201 202 203
print squares print unitlist print units print peers ['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'B1', 'B2', 省略, 'I9'] [['A1', 'B1', 省略 'H1', 'I1'], ['A2', 'B2', 省略 'H2', 'I2'], 省略 'I8', 'I9']] {'I6': [['A6', 省略 , 'I6'], ['I1', 省略, 'I9'], ['G4',省略 'I6']], 'H9': [[省略 {'I6': set(['I9', 'G6', 'G5', 'G4', 'F6', 'I8', 'I1', 'H6',省略]), 'H9': set([省略
"4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......" === 400000805 030000000 000700000 020000060 000080400 000010000 000603070 500200000 104000000 === 4 . . |. . . |8 . 5 . 3 . |. . . |. . . . . . |7 . . |. . . ------+------+------ . 2 . |. . . |. 6 . . . . |. 8 . |4 . . . . . |. 1 . |. . . ------+------+------ . . . |6 . 3 |. 7 . 5 . . |2 . . |. . . 1 . 4 |. . . |. . .
200 201
print grid_values(grid2) display(grid_values(grid2)) {'I6': '.',省略 'I3': '4', 'E5': '8', 'I5': 省略'B8': '.', 'B9': '.', 'D1': '.'} 4 . . |. . . |8 . 5 . 3 . |. . . |. . . . . . |7 . . |. . . ------+------+------ . 2 . |. . . |. 6 . . . . |. 8 . |4 . . . . . |. 1 . |. . . ------+------+------ . . . |6 . 3 |. 7 . 5 . . |2 . . |. . . 1 . 4 |. . . |. . .
200 201 202
display(parse_grid(grid1)) display(parse_grid(grid2)) display(parse_grid(hard1)) 4 8 3 |9 2 1 |6 5 7 9 6 7 |3 4 5 |8 2 1 2 5 1 |8 7 6 |4 9 3 ------+------+------ 5 4 8 |1 3 2 |9 7 6 7 2 9 |5 6 4 |1 3 8 1 3 6 |7 9 8 |2 4 5 ------+------+------ 3 7 2 |6 8 9 |5 1 4 8 1 4 |2 5 3 |7 6 9 6 9 5 |4 1 7 |3 8 2 4 1679 12679 | 139 2369 269 | 8 1239 5 26789 3 1256789 | 14589 24569 245689 | 12679 1249 124679 2689 15689 125689 | 7 234569 245689 | 12369 12349 123469 ------------------------+------------------------+------------------------ 3789 2 15789 | 3459 34579 4579 | 13579 6 13789 3679 15679 15679 | 359 8 25679 | 4 12359 12379 36789 4 56789 | 359 1 25679 | 23579 23589 23789 ------------------------+------------------------+------------------------ 289 89 289 | 6 459 3 | 1259 7 12489 5 6789 3 | 2 479 1 | 69 489 4689 1 6789 4 | 589 579 5789 | 23569 23589 23689 13478 1378 1478 | 124579 134579 6 | 1234579 123479 123579 13467 5 9 | 1247 1347 1247 | 123467 123467 8 2 1367 147 | 14579 134579 8 | 1345679 134679 13579 ---------------------------+---------------------------+--------------------------- 1789 4 5 | 126789 16789 1279 | 1236789 1236789 12379 1789 12789 3 | 12456789 1456789 12479 | 126789 126789 1279 1789 12789 6 | 12789 1789 3 | 12789 5 4 ---------------------------+---------------------------+--------------------------- 14789 1789 1478 | 3 2 5 | 14789 14789 6 13456789 1236789 12478 | 146789 146789 1479 | 12345789 1234789 123579 13456789 1236789 12478 | 146789 146789 1479 | 12345789 1234789 123579
200 201 202
values = dict((s, digits) for s in squares) s, d = 'G6', '3' display(assign(values, s, d)) 123456789 123456789 123456789 |123456789 123456789 12456789 |123456789 省略 123456789 123456789 123456789 |123456789 123456789 12456789 |123456789 省略 123456789 123456789 123456789 |123456789 123456789 12456789 |123456789 省略 ------------------------------+------------------------------+-------------- 123456789 123456789 123456789 |123456789 123456789 12456789 |123456789 省略 123456789 123456789 123456789 |123456789 123456789 12456789 |123456789 省略 123456789 123456789 123456789 |123456789 123456789 12456789 |123456789 省略 ------------------------------+------------------------------+-------------- 12456789 12456789 12456789 | 12456789 12456789 3 | 12456789 省略 123456789 123456789 123456789 | 12456789 12456789 12456789 |123456789 省略 123456789 123456789 123456789 | 12456789 12456789 12456789 |123456789 省略
57 58 201 元の
print s,d display(values) display(parse_grid(grid2)) 省略 F8 . 4 123679 123679 | 1239 2369 1269 | 8 1239 5 2356789 12356789 12356789| 1234589 234569 1245689 | 123679 12349 1234679 235689 1235689 1235689 | 7 234569 1245689 | 12369 12349 123469 ---------------------------+---------------------------+--------------------------- 235789 12345789 1235789 | 23459 234579 24579 | 123579 6 123789 235679 1235679 1235679 | 2359 8 25679 | 4 12359 12379 2356789 23456789 2356789 | 23459 1 245679 | 23579 23589 23789 ---------------------------+---------------------------+--------------------------- 2589 2589 2589 | 6 2459 3 | 1259 7 12489 2356789 2356789 2356789 | 124589 24579 1245789 | 123569 1234589 1234689 1 2356789 4 | 2589 2579 25789 | 23569 23589 23689 D2 2(上段の途中経過に'D2'に2を入れて進めたのが下段) 4 13679 123679 | 1239 2369 1269 | 8 1239 5 2356789 1356789 12356789| 1234589 234569 1245689 | 123679 12349 1234679 235689 135689 1235689 | 7 234569 1245689 | 12369 12349 123469 ---------------------------+---------------------------+--------------------------- 35789 2 135789 | 3459 34579 4579 | 13579 6 13789 35679 135679 135679 | 2359 8 25679 | 4 12359 12379 356789 4 356789 | 2359 1 25679 | 23579 23589 23789 ---------------------------+---------------------------+--------------------------- 2589 589 2589 | 6 2459 3 | 1259 7 12489 2356789 356789 2356789 | 124589 24579 1245789 | 123569 1234589 1234689 1 356789 4 | 2589 2579 25789 | 23569 23589 23689 省略
2 G2 89 2 G1 29 2 G5 45 2 H2 67 2 A2 19 2 A4 39 2 A8 29 2 A5 69 2 A5 26 2 A6 26 2 A4 13 2 A6 26 2 A5 26 2 A2 19 2 A4 39
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
## Solve Every Sudoku Puzzle ## See http://norvig.com/sudoku.html ## Throughout this program we have: ## r is a row, e.g. 'A' ## c is a column, e.g. '3' ## s is a square, e.g. 'A3' ## d is a digit, e.g. '9' ## u is a unit, e.g. ['A1','B1','C1','D1','E1','F1','G1','H1','I1'] ## grid is a grid,e.g. 81 non-blank chars, e.g. starting with '.18...7... ## values is a dict of possible values, e.g. {'A1':'12349', 'A2':'8', ...} def cross(A, B): "Cross product of elements in A and elements in B." return [a+b for a in A for b in B] digits = '123456789' rows = 'ABCDEFGHI' cols = digits squares = cross(rows, cols) unitlist = ([cross(rows, c) for c in cols] + [cross(r, cols) for r in rows] + [cross(rs, cs) for rs in ('ABC','DEF','GHI') for cs in ('123','456','789')]) units = dict((s, [u for u in unitlist if s in u]) for s in squares) peers = dict((s, set(sum(units[s],[]))-set([s])) for s in squares) ################ Unit Tests ################ ''' def test(): "A set of tests that must pass." assert len(squares) == 81 assert len(unitlist) == 27 assert all(len(units[s]) == 3 for s in squares) assert all(len(peers[s]) == 20 for s in squares) assert units['C2'] == [['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2'], ['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9'], ['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3']] assert peers['C2'] == set(['A2', 'B2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2', 'C1', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'A1', 'A3', 'B1', 'B3']) print('All tests pass.') ''' ################ Parse a Grid ################ def parse_grid(grid): """Convert grid to a dict of possible values, {square: digits}, or return False if a contradiction is detected.""" ## To start, every square can be any digit; then assign values from the grid. values = dict((s, digits) for s in squares) for s,d in grid_values(grid).items(): if d in digits and not assign(values, s, d): return False ## (Fail if we can't assign d to square s.) return values def grid_values(grid): "Convert grid into a dict of {square: char} with '0' or '.' for empties." chars = [c for c in grid if c in digits or c in '0.'] assert len(chars) == 81 return dict(zip(squares, chars)) ################ Constraint Propagation ################ def assign(values, s, d): """Eliminate all the other values (except d) from values[s] and propagate. Return values, except return False if a contradiction is detected.""" other_values = values[s].replace(d, '') if all(eliminate(values, s, d2) for d2 in other_values): return values else: return False def eliminate(values, s, d): """Eliminate d from values[s]; propagate when values or places <= 2. Return values, except return False if a contradiction is detected.""" if d not in values[s]: return values ## Already eliminated values[s] = values[s].replace(d,'') ## (1) If a square s is reduced to one value d2, then eliminate d2 from the peers. if len(values[s]) == 0: return False ## Contradiction: removed last value elif len(values[s]) == 1: d2 = values[s] if not all(eliminate(values, s2, d2) for s2 in peers[s]): return False ## (2) If a unit u is reduced to only one place for a value d, then put it there. for u in units[s]: dplaces = [s for s in u if d in values[s]] if len(dplaces) == 0: return False ## Contradiction: no place for this value elif len(dplaces) == 1: # d can only be in one place in unit; assign it there if not assign(values, dplaces[0], d): return False return values ################ Display as 2-D grid ################ def display(values): "Display these values as a 2-D grid." width = 1+max(len(values[s]) for s in squares) line = '+'.join(['-'*(width*3)]*3) for r in rows: print(''.join(values[r+c].center(width)+('|' if c in '36' else '') for c in cols)) if r in 'CF': print(line) print() ################ Search ################ def solve(grid): return search(parse_grid(grid)) def search(values): "Using depth-first search and propagation, try all possible values." if values is False: return False ## Failed earlier if all(len(values[s]) == 1 for s in squares): return values ## Solved! ## Chose the unfilled square s with the fewest possibilities n,s = min((len(values[s]), s) for s in squares if len(values[s]) > 1) return some(search(assign(values.copy(), s, d)) for d in values[s]) ################ Utilities ################ def some(seq): "Return some element of seq that is true." for e in seq: if e: return e return False def from_file(filename, sep='\n'): "Parse a file into a list of strings, separated by sep." # return file(filename).read().strip().split(sep) with open(filename, mode='r', encoding="utf-8") as f: return f.read().strip().split(sep) def shuffled(seq): "Return a randomly shuffled copy of the input sequence." seq = list(seq) random.shuffle(seq) return seq ################ System test ################ import time, random time.clock = time.time # 追加 def solve_all(grids, name='', showif=0.0): """Attempt to solve a sequence of grids. Report results. When showif is a number of seconds, display puzzles that take longer. When showif is None, don't display any puzzles.""" def time_solve(grid): start = time.clock() values = solve(grid) t = time.clock()-start ## Display puzzles that take long enough if showif is not None and t > showif: display(grid_values(grid)) if values: display(values) print('(%.2f seconds)\n' % t) return (t, solved(values)) times, results = zip(*[time_solve(grid) for grid in grids]) N = len(grids) if N > 1: print("Solved %d of %d %s puzzles \ (avg %.2f secs (%d Hz), max %.2f secs)." \ % (sum(results), N, name, sum(times)/N, N/sum(times), max(times))) def solved(values): "A puzzle is solved if each unit is a permutation of the digits 1 to 9." def unitsolved(unit): return set(values[s] for s in unit) == set(digits) return values is not False and all(unitsolved(unit) for unit in unitlist) def random_puzzle(N=17): """Make a random puzzle with N or more assignments. Restart on contradictions. Note the resulting puzzle is not guaranteed to be solvable, but empirically about 99.8% of them are solvable. Some have multiple solutions.""" values = dict((s, digits) for s in squares) for s in shuffled(squares): if not assign(values, s, random.choice(values[s])): break ds = [values[s] for s in squares if len(values[s]) == 1] if len(ds) >= N and len(set(ds)) >= 8: return ''.join(values[s] if len(values[s])==1 else '.' \ for s in squares) return random_puzzle(N) ## Give up and make a new puzzle grid1 = \ '003020600900305001001806400008102900700000008006708200002609500800203009005010300' grid2 = \ '4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......' hard1 = \ '.....6....59.....82....8....45........3........6..3.54...325..6..................' if __name__ == '__main__': # test() solve_all(from_file("easy50.txt", '========'), "easy", None) solve_all(from_file("top95.txt"), "hard", None) solve_all(from_file("hardest.txt"), "hardest", None) solve_all([random_puzzle() for _ in range(99)], "random", 100.0) # solve_all([grid1]) # 191行目 # solve_all([grid2]) # solve_all([hard1]) ## References used: #http://www.scanraid.com/BasicStrategies.htm #http://www.sudokudragon.com/sudokustrategy.htm #http://www.krazydad.com/blog/2005/09/29/an-index-of-sudoku-strategies/ #http://www2.warwick.ac.uk/fac/sci/moac/currentstudents/peter_cock/python/sudoku/