見出し画像

複数の答えのある数独・ナンプレ

 数独・ナンプレの問題をpythonを使って解く.ソースは一般公開しません.最初は,Gurobi Optimizerを使って解くようにしていたが,商用ソルバーを使わずに解けるよう,mypulpを使う.複数の答えがある場合にも,制約式を追加してすべて列挙するようにしている.例題として,初期配置が次の問題を対象とする.

初期配置

 本来の初期配置をいくつか削っているので,以下のように答えが複数でてくる.

1
+-------+-------+-------+
| 5 3 2 | 6 7 8 | 4 1 9 |
| 6 7 4 | 1 9 5 | 3 2 8 |
| 1 9 8 | 3 4 2 | 5 6 7 |
+-------+-------+-------+
| 8 2 9 | 7 6 4 | 1 5 3 |
| 4 1 6 | 8 5 3 | 7 9 2 |
| 7 5 3 | 9 2 1 | 8 4 6 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 6 3 5 |
| 3 4 5 | 2 8 6 | 9 7 1 |
+-------+-------+-------+
2
+-------+-------+-------+
| 5 3 2 | 6 7 8 | 4 1 9 |
| 6 7 4 | 1 9 5 | 8 3 2 |
| 1 9 8 | 3 4 2 | 7 6 5 |
+-------+-------+-------+
| 8 1 9 | 7 6 4 | 5 2 3 |
| 4 2 6 | 8 5 3 | 1 9 7 |
| 7 5 3 | 9 2 1 | 6 4 8 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 3 5 6 |
| 3 4 5 | 2 8 6 | 9 7 1 |
+-------+-------+-------+
3
+-------+-------+-------+
| 5 3 4 | 6 7 8 | 1 9 2 |
| 6 7 2 | 1 9 5 | 3 4 8 |
| 1 9 8 | 3 4 2 | 5 6 7 |
+-------+-------+-------+
| 8 5 9 | 7 6 1 | 4 2 3 |
| 4 2 6 | 8 5 3 | 7 1 9 |
| 7 1 3 | 9 2 4 | 8 5 6 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 6 3 5 |
| 3 4 5 | 2 8 6 | 9 7 1 |
+-------+-------+-------+
4
+-------+-------+-------+
| 5 3 2 | 6 7 8 | 1 4 9 |
| 6 7 4 | 1 9 5 | 3 2 8 |
| 1 9 8 | 3 4 2 | 5 6 7 |
+-------+-------+-------+
| 8 2 9 | 7 6 1 | 4 5 3 |
| 4 1 6 | 8 5 3 | 7 9 2 |
| 7 5 3 | 9 2 4 | 8 1 6 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 6 3 5 |
| 3 4 5 | 2 8 6 | 9 7 1 |
+-------+-------+-------+
5
+-------+-------+-------+
| 5 3 2 | 6 7 8 | 9 4 1 |
| 6 7 4 | 1 9 5 | 3 2 8 |
| 1 9 8 | 3 4 2 | 5 6 7 |
+-------+-------+-------+
| 8 2 9 | 7 6 1 | 4 5 3 |
| 4 1 6 | 8 5 3 | 7 9 2 |
| 7 5 3 | 9 2 4 | 8 1 6 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 6 3 5 |
| 3 4 5 | 2 8 6 | 1 7 9 |
+-------+-------+-------+
6
+-------+-------+-------+
| 5 3 4 | 6 7 8 | 9 2 1 |
| 6 7 2 | 1 9 5 | 3 4 8 |
| 1 9 8 | 3 4 2 | 5 6 7 |
+-------+-------+-------+
| 8 2 9 | 7 6 1 | 4 5 3 |
| 4 1 6 | 8 5 3 | 7 9 2 |
| 7 5 3 | 9 2 4 | 8 1 6 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 6 3 5 |
| 3 4 5 | 2 8 6 | 1 7 9 |
+-------+-------+-------+
7
+-------+-------+-------+
| 5 3 4 | 6 7 8 | 1 2 9 |
| 6 7 2 | 1 9 5 | 3 4 8 |
| 1 9 8 | 3 4 2 | 5 6 7 |
+-------+-------+-------+
| 8 2 9 | 7 6 1 | 4 5 3 |
| 4 1 6 | 8 5 3 | 7 9 2 |
| 7 5 3 | 9 2 4 | 8 1 6 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 6 3 5 |
| 3 4 5 | 2 8 6 | 9 7 1 |
+-------+-------+-------+
8
+-------+-------+-------+
| 5 3 4 | 6 7 8 | 1 9 2 |
| 6 7 2 | 1 9 5 | 3 4 8 |
| 1 9 8 | 3 4 2 | 5 6 7 |
+-------+-------+-------+
| 8 2 9 | 7 6 1 | 4 5 3 |
| 4 1 6 | 8 5 3 | 7 2 9 |
| 7 5 3 | 9 2 4 | 8 1 6 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 6 3 5 |
| 3 4 5 | 2 8 6 | 9 7 1 |
+-------+-------+-------+
9
+-------+-------+-------+
| 5 3 4 | 6 7 8 | 9 1 2 |
| 6 7 2 | 1 9 5 | 3 4 8 |
| 1 9 8 | 3 4 2 | 5 6 7 |
+-------+-------+-------+
| 8 5 9 | 7 6 1 | 4 2 3 |
| 4 2 6 | 8 5 3 | 7 9 1 |
| 7 1 3 | 9 2 4 | 8 5 6 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 6 3 5 |
| 3 4 5 | 2 8 6 | 1 7 9 |
+-------+-------+-------+
10
+-------+-------+-------+
| 5 3 2 | 6 7 8 | 4 9 1 |
| 6 7 4 | 1 9 5 | 8 3 2 |
| 1 9 8 | 3 4 2 | 7 6 5 |
+-------+-------+-------+
| 8 1 9 | 7 6 4 | 5 2 3 |
| 4 2 6 | 8 5 3 | 9 1 7 |
| 7 5 3 | 9 2 1 | 6 4 8 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 3 5 6 |
| 3 4 5 | 2 8 6 | 1 7 9 |
+-------+-------+-------+
11
+-------+-------+-------+
| 5 3 2 | 6 7 8 | 4 9 1 |
| 6 7 4 | 1 9 5 | 8 3 2 |
| 1 9 8 | 3 4 2 | 7 6 5 |
+-------+-------+-------+
| 8 2 9 | 7 6 4 | 5 1 3 |
| 4 1 6 | 8 5 3 | 9 2 7 |
| 7 5 3 | 9 2 1 | 6 4 8 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 3 5 6 |
| 3 4 5 | 2 8 6 | 1 7 9 |
+-------+-------+-------+
12
+-------+-------+-------+
| 5 3 2 | 6 7 8 | 4 9 1 |
| 6 7 4 | 1 9 5 | 8 3 2 |
| 1 9 8 | 3 4 2 | 7 6 5 |
+-------+-------+-------+
| 8 2 9 | 7 6 1 | 5 4 3 |
| 4 1 6 | 8 5 3 | 9 2 7 |
| 7 5 3 | 9 2 4 | 6 1 8 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 3 5 6 |
| 3 4 5 | 2 8 6 | 1 7 9 |
+-------+-------+-------+

Pythonのライブラリを使って数独・ナンプレを解くに紹介したpy-sudokuを使って,上記の問題と解くと,次のように1つしか答えを出さない.

いいなと思ったら応援しよう!