Cooci

python版本： 3.5.4

# 代码实现

``````
# 点类
class point():
def __init__(self, x, y):
self.x = x
self.y = y
self.available = []
self.value = 0

# 该空格所在行有哪些数
def rowNum(p ,sudoku):
# set用于去重，因为0不止一个！
row = set(sudoku[p.y*9: (p.y+1)*9])
row.remove(0)
return row

# 该空格所在列有哪些数
def colNum(p, sudoku):
col = []
length = len(sudoku)
for j in range(p.x, length, 9):
col.append(sudoku[j])
col = set(col)
col.remove(0)
return col

# 该空格所在小九宫有哪些数
def blockNum(p, sudoku):
block_x = p.x//3
block_y = p.y//3
block = []
start_point = block_y*3*9 + block_x*3
for j in range(start_point, start_point+3):
block.append(sudoku[j])
for j in range(start_point+9, start_point+9+3):
block.append(sudoku[j])
for j in range(start_point+9+9, start_point+9+9+3):
block.append(sudoku[j])
block = set(block)
block.remove(0)
return block

# 初始化，作用为：
# 把每个空格可能的点先列举出来
# 比如空格所在的行和列还有小九宫内有数字1、2、3
# 那么空格只能填入4、5、6、7、8、9中的某个数
def initialize(sudoku):
sudokuList = []
length = len(sudoku)
for index in range(length):
# 找到需要填入的单元，即空格
if sudoku[index] == 0:
p = point(index%9, index//9)
for i in range(1, 10):
# 如果行、列、小九宫中均没有i这个数
if (i not in rowNum(p, sudoku)) and (i not in colNum(p, sudoku)) and (i not in blockNum(p, sudoku)):
p.available.append(i)
sudokuList.append(p)
return sudokuList

# 检验该数填入空格后是否满足数独规则
def check(p, sudoku):
if p.value == 0:
return False
if (p.value not in rowNum(p, sudoku)) and (p.value not in colNum(p, sudoku)) and (p.value not in blockNum(p, sudoku)):
return True
else:
return False

# 展示数独结果
def showResult(sudoku):
for r in range(9):
for c in range(9):
print('%d ' % (sudoku[r*9+c]), end='')
print('')

# 深搜来解数独
def solve(p, sudoku):
available_Num = p.available
for ava in available_Num:
p.value = ava
if check(p, sudoku):
sudoku[p.y*9+p.x] = p.value
if len(sudokuList) < 1:
showResult(sudoku)
exit()
p_next = sudokuList.pop()
solve(p_next, sudoku)
sudoku[p_next.y*9+p_next.x] = 0
sudoku[p.y*9+p.x] = 0
p_next.value = 0
sudokuList.append(p_next)
else:
pass

if __name__ == '__main__':
# 0代表需要填入的单元，即空格
sudoku = [
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 9, 3, 6, 2, 8, 1, 4, 0,
0, 6, 0, 0, 0, 0, 0, 5, 0,
0, 3, 0, 0, 1, 0, 0, 9, 0,
0, 5, 0, 8, 0, 2, 0, 7, 0,
0, 4, 0, 0, 7, 0, 0, 6, 0,
0, 8, 0, 0, 0, 0, 0, 3, 0,
0, 1, 7, 5, 9, 3, 4, 2, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
]
sudokuList = initialize(sudoku)
print('数独题目为：\n')
showResult(sudoku)
print('\n数独的解为：\n')
p_first = sudokuList.pop()
solve(p_first, sudoku)``````

340 声望
28 粉丝
0 条评论