一个用AC-3算法求解数独的程序

github存储库

将python代码复制到同一个目录,比如C:\d\ac3, 然后用如下命令行运行

复制代码
C:\d\ac3>python driver.py
Would you like to use a preloaded Sudoku board?: (y/n)y
Choose one of the following levels:
[1] No Solution
[2] Easy
[3] Medium
[4] Hard
[5] Hardest in the World
Please input the level of difficulty: 1

8 - - |- - - |- - -
- - 3 |6 - - |- - -
- 7 - |- 9 - |2 - -
-------------------
- 5 - |- - 7 |- - -
- - - |- 4 5 |7 - -
- - - |1 - - |- 3 -
-------------------
- - 1 |- - - |- 6 8
- - 8 |5 - - |- 1 -
- 9 9 |9 9 9 |9 9 9

Error: There is no solution to this sudoku puzzle


C:\d\ac3>python driver.py
Would you like to use a preloaded Sudoku board?: (y/n)y
Choose one of the following levels:
[1] No Solution
[2] Easy
[3] Medium
[4] Hard
[5] Hardest in the World
Please input the level of difficulty: 5

8 - - |- - - |- - -
- - 3 |6 - - |- - -
- 7 - |- 9 - |2 - -
-------------------
- 5 - |- - 7 |- - -
- - - |- 4 5 |7 - -
- - - |1 - - |- 3 -
-------------------
- - 1 |- - - |- 6 8
- - 8 |5 - - |- 1 -
- 9 - |- - - |4 - -


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

Solve Time: 0.7645819187164307 seconds

C:\d\ac3>python driver.py
Would you like to use a preloaded Sudoku board?: (y/n)y
Choose one of the following levels:
[1] No Solution
[2] Easy
[3] Medium
[4] Hard
[5] Hardest in the World
Please input the level of difficulty: 4

- 7 - |- 4 2 |- - -
- - - |- - 8 |6 1 -
3 9 - |- - - |- - 7
-------------------
- - - |- - 4 |- - 9
- - 3 |- - - |7 - -
5 - - |1 - - |- - -
-------------------
8 - - |- - - |- 7 6
- 5 4 |8 - - |- - -
- - - |6 1 - |- 5 -


1 7 6 |3 4 2 |9 8 5
4 2 5 |9 7 8 |6 1 3
3 9 8 |5 6 1 |4 2 7
-------------------
2 6 1 |7 8 4 |5 3 9
9 8 3 |2 5 6 |7 4 1
5 4 7 |1 9 3 |2 6 8
-------------------
8 1 9 |4 2 5 |3 7 6
6 5 4 |8 3 7 |1 9 2
7 3 2 |6 1 9 |8 5 4

Solve Time: 0.1383507251739502 seconds

C:\d\ac3>python driver.py
Would you like to use a preloaded Sudoku board?: (y/n)y
Choose one of the following levels:
[1] No Solution
[2] Easy
[3] Medium
[4] Hard
[5] Hardest in the World
Please input the level of difficulty: 3

- - - |5 4 - |- - 8
6 - - |- - 2 |3 - -
- - 7 |- - 3 |- 9 -
-------------------
- 3 1 |- 5 - |- 2 -
- - - |- - - |- - -
- 4 - |- 3 - |7 1 -
-------------------
- 9 - |7 - - |2 - -
- - 8 |6 - - |- - 5
1 - - |- 2 4 |- - -


2 1 3 |5 4 9 |6 7 8
6 8 9 |1 7 2 |3 5 4
4 5 7 |8 6 3 |1 9 2
-------------------
9 3 1 |4 5 7 |8 2 6
8 7 2 |9 1 6 |5 4 3
5 4 6 |2 3 8 |7 1 9
-------------------
3 9 4 |7 8 5 |2 6 1
7 2 8 |6 9 1 |4 3 5
1 6 5 |3 2 4 |9 8 7

Solve Time: 0.02900862693786621 seconds

C:\d\ac3>
相关推荐
BothSavage4 小时前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn4 小时前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法
烬羽5 小时前
字符串算法入门:从反转字符串到回文判断,面试不再慌
算法·面试
先吃饱再说21 小时前
判断回文字符串,从一行代码到双指针优化
算法
黄敬峰1 天前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术1 天前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六1 天前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术1 天前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
Asize1 天前
初识DFS 与 BFS:递归、队列与图遍历
算法