圆月山庄资源网 Design By www.vgjia.com
网上看到一个python写的数独,很好玩,分享给大家。
import random import itertools from copy import deepcopy def make_board(m = 3): numbers = list(range(1, m**2 + 1)) board = None while board is None: board = attempt_board(m, numbers) return board def attempt_board(m, numbers): n = m**2 board = [[None for _ in range(n)] for _ in range(n)] for i, j in itertools.product(range(n), repeat = 2): i0, j0 = i - i % m, j - j % m random.shuffle(numbers) for x in numbers: if(x not in board[i]) and all(row[j] != x for row in board) and all(x not in row[j0:j0+m] for row in board[i0:i]): board[i][j] = x break else: return None return board def print_board(board, m = 3): numbers = list(range(1, m**2 + 1)) omit = 5 challange = deepcopy(board) for i, j in itertools.product(range(omit), range(m ** 2)): x = random.choice(numbers) - 1 challange[x][j] = None spacer = "++---+---+---++---+---+---++---+---+---++" print (spacer.replace('-', '=')) for i, line in enumerate(challange): print("|| {0} | {1} | {2} || {3} | {4} | {5} || {6} | {7} | {8} ||".format(*(cell or ' ' for cell in line))) if(i + 1) % 3 == 0: print(spacer.replace('-', '=')) else: print(spacer) return challange def print_answer(board): spacer = "++---+---+---++---+---+---++---+---+---++" print(spacer.replace('-','=')) for i, line in enumerate(board): print("|| {0} | {1} | {2} || {3} | {4} | {5} || {6} | {7} | {8} ||".format(*(cell or ' ' for cell in line))) if(i + 1) % 3 == 0: print(spacer.replace('-','=')) else: print(spacer) def is_full(challange, m = 3): for i, j in itertools.product(range(m**2), repeat = 2): if challange[i][j] is None: return False return True def cal_candidate(challange, x, y, m = 3): candidate = range(1, m ** 2 + 1) for i in range(m ** 2): if challange[x][i] in candidate: candidate.remove(challange[x][i]) if challange[i][y] in candidate: candidate.remove(challange[i][y]) for i, j in itertools.product(range(m), repeat = 2): x0, y0 = x - x % m, y - y % m if challange[x0 + i][y0 + j] in candidate: candidate.remove(challange[x0 + i][y0 + j]) return candidate def least_candidate(challange, m = 3): least, x, y = m ** 2, -1, -1 for i, j in itertools.product(range(m ** 2), repeat = 2): if not challange[i][j]: num = len(cal_candidate(challange, i, j)) if num < least: least = num x, y = i, j return x, y def solving_soduku(challange, m = 3): if is_full(challange): return challange x, y = least_candidate(challange) id = x * (m ** 2) + y result = try_candidate(challange, id) return result def try_candidate(challange, id, m = 3): if is_full(challange): return challange x = id / (m ** 2) y = id % (m ** 2) while challange[x][y]: id = (id + 1) % m ** 4 x = id / (m ** 2) y = id % (m ** 2) candidate = cal_candidate(challange, x, y) if len(candidate) == 0: return False for i in range(len(candidate)): challange[x][y] = candidate[i] result_r = try_candidate(challange, (id + 1) % m ** 4) if not result_r: pass else: return challange challange[x][y] = None return False #Board = make_board() #print Board #challange = print_board(Board) #print_answer(Board) #result = solving_soduku(challange) #print_answer(result) testing = [[8, None, None, None, None, None, None, None, None], [None, None, 3, 6, None, None, None, None, None], [None, 7, None, None, 9, None, 2, None, None], [None,5 , None, None, None, 7, None, None, None ], [None, None, None, None, 4, 6, 7, None, None], [None, None, None, 1, None, None, None, 3, None], [None, None, 1, None, None, None, None, 6, 8], [None, None, 8, 5, None, None, None, 1, None], [None, 9, None, None, None, None, 4, None, None]] result = solving_soduku(testing) print_answer(result)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年11月07日
2024年11月07日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]