这两天一直在研究数独,但是并没有研究透彻,由于时间的原因,暂时研究终止。期间一共写出了一个大方法,一个小方法。大方法(m1)已经完善,小方法(m2)还待优化。
注意一点:输入数据后,先执行一次"up"命令,更新一下每个棋盘格的可选数字,才可执行"m1"或"m2"的操作。
代码就如下所示:
class shuDu: # 一道待解决的数独 cBoard =[ [6,0,0,0,0,0,5,8,0], [5,2,0,9,0,0,6,0,0], [0,0,0,0,5,0,0,0,3], [2,0,0,8,3,0,0,0,0], [0,0,6,1,0,9,3,0,0], [0,0,0,0,7,6,0,0,9], [8,0,0,0,4,0,0,0,0], [0,0,2,0,0,5,0,1,7], [0,6,1,0,0,0,0,0,4] ] check = {} #可用数字字典 def refresh(self): print(self.cBoard) print("Refresh over") def show(self): print("==========================") for i in range(0,9): for j in range(0,9): num = ['-','-','-','-','-','-','-','-','-'] if((i,j) in self.check): for x in range(len(self.check[(i,j)])): num[x] = list(self.check[(i,j)])[x] print(num[0],num[1],num[2],num[3],num[4],num[5],num[6],num[7],num[8],end=" ") if(j==2 or j==5): print(end=" ") print("") if(i==2 or i==5): print("") print("==========================") def change(self): print("输入为“空”宫格的行和列以及替换值(\\退出):") while(1): row,column,value = input() #行 if(row == "/"): return row = eval(row) column = eval(column) value = eval(value) self.cBoard[row][column] = value print("Change over") def dele(self): row,column,value = input("输入要删除的数字:") #行 row = eval(row) column = eval(column) value = eval(value) self.check[(row,column)].remove(value) print("Change over") def inputData(self): print("Enter data:") for index in range(9): a,b,c,d,e,f,g,h,i = input() self.cBoard[index] = [eval(a),eval(b),eval(c),eval(d),eval(e),eval(f),eval(g),eval(h),eval(i)] print("Enter over") def checkBoard(self): for i in self.check: print(i,self.check[i]) print("Print over") def update(self): self.check = {} for row in range(0,9): for column in range(0,9): if(self.cBoard[row][column]==0): #找到一个为空的宫格 s = {1,2,3,4,5,6,7,8,9} for i in range(9): s.discard(self.cBoard[row][i]) # 移除集合中的元素,且如果元素不存在,不会发生错误 s.discard(self.cBoard[i][column]) if( row<3 and column<3): #i-row j->column 移除九宫格的元素 for i in range(0,3): for j in range(0,3): s.discard(self.cBoard[i][j]) elif( 3<=row<6 and column<3 ): for i in range(3,6): for j in range(0,3): s.discard(self.cBoard[i][j]) elif ( 6<=row<9 and column<3 ): for i in range(6,9): for j in range(0,3): s.discard(self.cBoard[i][j]) elif ( row<3 and 3<=column<6 ): for i in range(0,3): for j in range(3,6): s.discard(self.cBoard[i][j]) elif ( 3<=row<6 and 3<=column<6): for i in range(3,6): for j in range(3,6): s.discard(self.cBoard[i][j]) elif ( 6<=row<9 and 3<=column<6): for i in range(6,9): for j in range(3,6): s.discard(self.cBoard[i][j]) elif ( row<3 and 6<=column<9): for i in range(0,3): for j in range(6,9): s.discard(self.cBoard[i][j]) elif ( 3<=row<6 and 6<=column<9): for i in range(3,6): for j in range(6,9): s.discard(self.cBoard[i][j]) elif ( 6<=row<9 and 6<=column<9 ): for i in range(6,9): for j in range(6,9): s.discard(self.cBoard[i][j]) self.check[(row,column)]= s def method1(self): #基础摒除法 print("==========================") for i in self.check: s = self.check[i] if(len(s)==1): self.cBoard[i[0]][i[1]] = list(s)[0] self.update() print(i,s) print("Deal over") print("==========================") def method2(self): #互斥法 print("==========================") for row in range(0,9): #行 for column in range(0,9): if(self.cBoard[row][column]==0): bads = set() for j in range(0,9): if(self.cBoard[row][j]==0 and j!= column ): #逻辑运算符出错 bads = bads.union(self.check[(row,j)]) # add set goods = self.check[(row,column)].difference(bads) if(len(goods)!=0): self.cBoard[row][column] = list(goods)[0] self.update() print(row,column,goods,"row") for column in range(0,9): #列 for row in range(0,9): if(self.cBoard[row][column]==0): bads = set() for i in range(0,9): if(self.cBoard[i][column]==0 and i!= row ): #逻辑运算符出错 bads = bads.union(self.check[(i,column)]) # add set goods = self.check[(row,column)].difference(bads) if(len(goods)!=0): self.cBoard[row][column] = list(goods)[0] self.update() print(row,column,goods,"column") for row in range(0,3): #九宫格 for column in range(0,3): if(self.cBoard[row][column]==0): bads = set() for i in range(0,3): for j in range(0,3): if(self.cBoard[i][j]==0 and (i!= row or j!= column) ): #逻辑运算符出错 bads = bads.union(self.check[(i,j)]) # add set goods = self.check[(row,column)].difference(bads) if(len(goods)!=0): self.cBoard[row][column] = list(goods)[0] self.update() print(row,column,goods) for row in range(0,3): for column in range(3,6): if(self.cBoard[row][column]==0): bads = set() for i in range(0,3): for j in range(3,6): if(self.cBoard[i][j]==0 and (i!= row or j!= column) ): #逻辑运算符出错 bads = bads.union(self.check[(i,j)]) # add set goods = self.check[(row,column)].difference(bads) if(len(goods)!=0): self.cBoard[row][column] = list(goods)[0] self.update() print(row,column,goods) for row in range(0,3): for column in range(6,9): if(self.cBoard[row][column]==0): bads = set() for i in range(0,3): for j in range(6,9): if(self.cBoard[i][j]==0 and (i!= row or j!= column) ): #逻辑运算符出错 bads = bads.union(self.check[(i,j)]) # add set goods = self.check[(row,column)].difference(bads) if(len(goods)!=0): self.cBoard[row][column] = list(goods)[0] self.update() print(row,column,goods) for row in range(3,6): for column in range(0,3): if(self.cBoard[row][column]==0): bads = set() for i in range(3,6): for j in range(0,3): if(self.cBoard[i][j]==0 and (i!= row or j!= column) ): #逻辑运算符出错 bads = bads.union(self.check[(i,j)]) # add set goods = self.check[(row,column)].difference(bads) if(len(goods)!=0): self.cBoard[row][column] = list(goods)[0] self.update() print(row,column,goods) for row in range(3,6): for column in range(3,6): if(self.cBoard[row][column]==0): bads = set() for i in range(3,6): for j in range(3,6): if(self.cBoard[i][j]==0 and (i!= row or j!= column) ): #逻辑运算符出错 bads = bads.union(self.check[(i,j)]) # add set goods = self.check[(row,column)].difference(bads) if(len(goods)!=0): self.cBoard[row][column] = list(goods)[0] self.update() print(row,column,goods) for row in range(3,6): for column in range(6,9): if(self.cBoard[row][column]==0): bads = set() for i in range(3,6): for j in range(6,9): if(self.cBoard[i][j]==0 and (i!= row or j!= column) ): #逻辑运算符出错 bads = bads.union(self.check[(i,j)]) # add set goods = self.check[(row,column)].difference(bads) if(len(goods)!=0): self.cBoard[row][column] = list(goods)[0] self.update() print(row,column,goods) for row in range(6,9): for column in range(0,3): if(self.cBoard[row][column]==0): bads = set() for i in range(6,9): for j in range(0,3): if(self.cBoard[i][j]==0 and (i!= row or j!= column) ): #逻辑运算符出错 bads = bads.union(self.check[(i,j)]) # add set goods = self.check[(row,column)].difference(bads) if(len(goods)!=0): self.cBoard[row][column] = list(goods)[0] self.update() print(row,column,goods) for row in range(6,9): for column in range(3,6): if(self.cBoard[row][column]==0): bads = set() for i in range(6,9): for j in range(3,6): if(self.cBoard[i][j]==0 and (i!= row or j!= column) ): #逻辑运算符出错 bads = bads.union(self.check[(i,j)]) # add set goods = self.check[(row,column)].difference(bads) if(len(goods)!=0): self.cBoard[row][column] = list(goods)[0] self.update() print(row,column,goods) for row in range(6,9): for column in range(6,9): if(self.cBoard[row][column]==0): bads = set() for i in range(6,9): for j in range(6,9): if(self.cBoard[i][j]==0 and (i!= row or j!= column) ): #逻辑运算符出错 bads = bads.union(self.check[(i,j)]) # add set goods = self.check[(row,column)].difference(bads) if(len(goods)!=0): self.cBoard[row][column] = list(goods)[0] self.update() print(row,column,goods) print("Deal over") print("==========================") '''--------------------------------------------------------------------------------------''' ex = shuDu() while(1): judge = input("-r打印棋盘 \ \n-c改变一个棋盘的值“123”将(1,2)改为3 \ \n-m1使用法则1处理 \ \n-ch打印空棋盘的可选数字 \ \n-m2使用法则2处理 \ \n-in输入数据“001020003” \ \n-up更新空棋盘的可选数字 \ \n-sh可选数字的方式打印棋盘 \ \n-de从可选数字中删除一个数字 “123” (1,2)中删除3 \ \n") if(judge == "r"): ex.refresh() elif(judge =="c"): ex.change() elif(judge =="m1"): ex.method1() elif(judge =="ch"): ex.checkBoard() elif(judge == "m2"): ex.method2() elif(judge == "in"): ex.inputData() elif(judge == "up"): ex.update() elif(judge == "sh"): ex.show() elif(judge == "de"): ex.dele() else: print("Enter error")
题中数据是一道正在解决的题目,下边是正在进行的研究进度:
下边是该题的答案:
等以后有时间和精力,再去研究吧,暂且搁置于此。