python生成数独_数独生成与求解,Python实现

2.[文件]

Generate.py ~ 3KB

下载(44)

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# written by Kurt Wang (nirvanawgw@gmail.com)

from random import choice

import random

class Generate():

"""

Generate Sudoku written by Kurt Wang

"""

NORMS = 9

CELL = 3

EMPTY = 0

sudoku_copy = [[EMPTY for x in range(NORMS)] for y in range(NORMS)]

def printSudoku(self, sudoku):

"""

print Sudoku as human readable

"""

text = ''

for row in range(self.NORMS):

for col in range(self.NORMS):

text = text + str(sudoku[row][col]) + ' '

if (col + 1) % self.NORMS == 0:

text = text + '\n'

print text

def generateGame(self, removeCount):

"""

generate a Sudoku game

"""

sudoku = self.generateSudoku()

for i in range(removeCount):

row = random.randint(0, self.NORMS - 1)

col = random.randint(0, self.NORMS - 1)

sudoku[row][col] = self.EMPTY

self.printSudoku(sudoku)

return sudoku

def generateSudoku(self):

"""

geneate Sudoku

"""

# create empty Sudoku

sudoku = [[self.EMPTY for x in range(self.NORMS)] for y in range(self.NORMS)]

# save current un-modify status

self.copySudoku(sudoku, self.sudoku_copy)

while(not self.generate(sudoku)):

pass

self.printSudoku(sudoku)

return sudoku

def generate(self, sudoku):

"""

genearte Sudoku, if there is no suitable constraints, return False

"""

# reset sudoku to un-modify

sudoku = self.copySudoku(self.sudoku_copy, sudoku)

for row in range(self.NORMS):

for col in range(self.NORMS):

# get constraints for point sudoku[row][col]

constraints = self.getConstraints(sudoku, row, col)

if len(constraints) == 0:

# nothing to select, return False

return False

# random select one from constraints

sudoku[row][col] = int(choice(constraints))

return True

def copySudoku(self, source, dist):

"""

create a deep copy of sudoku

"""

for row in range(self.NORMS):

for col in range(self.NORMS):

dist[row][col] = source[row][col]

return dist

def getConstraints(self, sudoku, row, col):

"""

return constraints for given row, col

"""

constraints = set([1, 2, 3, 4, 5, 6, 7, 8, 9])

# remove row

constraints = self.removeRowConflict(sudoku, constraints, row)

# remove col

constraints = self.removeColConflict(sudoku, constraints, col)

# remove 3x3 cell

constraints = self.removeCellConflict(sudoku, constraints, row, col)

return list(constraints)

def removeRowConflict(self, sudoku, constraints, row):

"""

remove confilct in row

"""

return constraints - set(sudoku[row])

def removeColConflict(self, sudoku, constraints, col):

"""

remove confilct in col

"""

colList = []

for i in range(self.NORMS):

for j in range(self.NORMS):

if j == col:

colList.append(sudoku[i][j])

return constraints - set(colList)

def removeCellConflict(self, sudoku, constraints, row, col):

"""

remove confilct in cell

"""

cellList = []

# cell start index

rowCell = row / self.CELL * self.CELL

colCell = col / self.CELL * self.CELL

for i in range(rowCell, rowCell + self.CELL):

for j in range(colCell, colCell + self.CELL):

cellList.append(sudoku[i][j])

return constraints - set(cellList)

if __name__ == "__main__":

generate = Generate()

generate.generateGame(80)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值