编程笼统的来说,就是个方法论,不论什么程序,都必须将问题的解决过程分解成计算机可以实现的若干个简单方法。俗话说,大道至简。对于只能明白0和1的计算机来说,就更需要细分步骤,一步一步的解决问题了。
首先来思考一下解数独的基本概念。
数独横九竖九共八十一个格子,同时又分为9个九宫格。规则很简单:需要保证每个横排和竖排以及九宫格内无相同数字。
第一种思路是对空格遍历所有数寻找结果:从第一个空格从 1 开始填开始填数,如果 1 不满足横排竖排九宫格无重复的话,就再填入 2 ,以此类推,直到填入一个暂时满足规则的数,中断此格,移动到下一个空格重复这个过程。如果到达某个空格发现已经无数可选了,说明前面某一格填错了,那就返回上一格,从上一格的中断处继续往 9 尝试,直到这样回溯到填错的那一格。
# -*- coding: utf-8 -*-
# author: zjp
# 输入芬兰数学家因卡拉花费3个月时间设计出的世界上迄今难度最大的数独:
# 将空格用 . 表示,同时将数独表示成嵌套的列表list,这样每格的行数和列数就正好是列表中每个对应数的索引
import datetime
sudoku1 = [
["5", "3", ".", " .", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
]
sudoku2 = [
[8,0,0,0,0,0,0,0,0],
[0,0,3,6,0,0,0,0,0],
[0,7,0,0,9,0,2,0,0],
[0,5,0,0,0,7,0,0,0],
[0,0,0,8,4,5,7,0,0],
[0,0,0,1,0,0,0,3,0],
[0,0,1,0,0,0,0,6,8],
[0,0,8,5,0,0,0,1,0],
[0,9,0,0,0,0,4,0,0]
]
def wr_sudoku(board): # 判断数独是否符合条件
# 判断一行是否有效
for i in range(9):
for j in board[i]:
if (j != '.') and (board[i].count(j) > 1):
return False
# 判断一列是否有效
column = [k[i] for k in board]
for n in column:
if (n != '.') and (board[i].count[n] > 1):
return False
# 判断九宫格是否有效
for i in range(3):
for j in range(3):
grid = [tem[j * 3:(j + 1) * 3] for tem in board[i * 3:(i + 1) * 3]]
merge_str = grid[0] + grid[1] + grid[2] #合并为一个list[]
for m in merge_str:
if (m != '.') and (merge_str.count(m) > 1):
return False
return True
class fill_sudoku(object):
def __init__(self, board):
self.b = board
self.t = 0
def check(self, x, y, value): # 检查每行每列及每九宫是否有相同项
for row_item in self.b[x]:
if row_item == value:
return False
for row_all in self.b:
if row_all[y] == value:
return False
row, col = x/3*3, y/3*3
row3col3 = self.b[row][col:col+3]+self.b[row+1][col:col+3]+self.b[row+2][col:col+3]
for row3col3_item in row3col3:
if row3col3_item == value:
return False
return True
def get_next(self, x, y): # 得到下一个未填项
for next_soulu in range(y+1, 9):
if self.b[x][next_soulu] == 0:
return x, next_soulu
for row_n in range(x+1, 9):
for col_n in range(0, 9):
if self.b[row_n][col_n] == 0:
return row_n, col_n
return -1, -1 #若无下一个未填项,返回-1
def try_it(self, x, y): # 主循环
if self.b[x][y] == 0:
for i in range(1, 10): # 从1到9尝试
self.t += 1
if self.check(x, y, i): # 符合 行列宫均无条件 的
self.b[x][y] = i #将符合条件的填入0格
next_x, next_y = self.get_next(x, y) #得到下一个0格
if next_x == -1: #如果无下一个0格
return True #返回True
else: #如果有下一个0格,递归判断下一个0格直到填满数独
end=self.try_it(next_x, next_y)
if not end: #在递归过程中存在不符合条件的,即 使try_it函数返回None的项
self.b[x][y] = 0 #回朔到上一层继续
else:
return True
def start(self):
begin = datetime.datetime.now()
if self.b[0][0] == 0:
self.try_it(0, 0)
else:
x, y = self.get_next(0, 0)
self.try_it(x, y)
for i in self.b:
print(i)
end = datetime.datetime.now()
print('cost time: ', end - begin)
print('times: ', self.t)
return
s = fill_sudoku(sudoku2)
s.start()
虽然python求解数独已经很快了,但是Java好像可以更快,有兴趣可以试一试。