最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

簡單實(shí)現(xiàn)python數(shù)獨(dú)游戲

 更新時(shí)間:2018年03月30日 15:28:02   作者:單鵬飛  
這篇文章主要為大家詳細(xì)介紹了如何簡單實(shí)現(xiàn)python數(shù)獨(dú)游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

網(wǎng)上看到一個(gè)python寫的數(shù)獨(dú),很好玩,分享給大家。

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)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python的pytest框架之命令行參數(shù)詳解(下)

    python的pytest框架之命令行參數(shù)詳解(下)

    這篇文章主要介紹了python的pytest框架之命令行參數(shù)詳解,今天將繼續(xù)更新其他一些命令選項(xiàng)的使用,和pytest收集測試用例的規(guī)則,需要的朋友可以參考下
    2019-06-06
  • python處理大數(shù)字的方法

    python處理大數(shù)字的方法

    這篇文章主要介紹了python處理大數(shù)字的方法,涉及Python遞歸操作的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Python+Django+MySQL實(shí)現(xiàn)基于Web版的增刪改查的示例代碼

    Python+Django+MySQL實(shí)現(xiàn)基于Web版的增刪改查的示例代碼

    這篇文章主要介紹了Python+Django+MySQL實(shí)現(xiàn)基于Web版的增刪改查的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Python Numpy 數(shù)組的初始化和基本操作

    Python Numpy 數(shù)組的初始化和基本操作

    Python 是一種高級的,動(dòng)態(tài)的,多泛型的編程語言。接下來通過本文給大家介紹Python Numpy 數(shù)組的初始化和基本操作,感興趣的朋友一起看看吧
    2018-03-03
  • Python中enumerate函數(shù)及其應(yīng)用詳解

    Python中enumerate函數(shù)及其應(yīng)用詳解

    在 Python 編程中,enumerate 函數(shù)是一個(gè)非常實(shí)用的工具,它能夠?qū)⒁粋€(gè)可迭代對象組合為一個(gè)索引序列,同時(shí)列出數(shù)據(jù)和數(shù)據(jù)下標(biāo),這種功能在處理列表、元組、字符串等可迭代對象時(shí)非常有用,尤其是在需要同時(shí)獲取每個(gè)元素的索引和值的情況下,需要的朋友可以參考下
    2025-01-01
  • 基于Python實(shí)現(xiàn)將列表數(shù)據(jù)生成折線圖

    基于Python實(shí)現(xiàn)將列表數(shù)據(jù)生成折線圖

    這篇文章主要介紹了如何利用Python中的pandas庫和matplotlib庫,實(shí)現(xiàn)將列表數(shù)據(jù)生成折線圖,文中的示例代碼簡潔易懂,需要的可以參考一下
    2022-03-03
  • 基于Numpy.convolve使用Python實(shí)現(xiàn)滑動(dòng)平均濾波的思路詳解

    基于Numpy.convolve使用Python實(shí)現(xiàn)滑動(dòng)平均濾波的思路詳解

    這篇文章主要介紹了Python極簡實(shí)現(xiàn)滑動(dòng)平均濾波(基于Numpy.convolve)的相關(guān)知識,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • 詳解基于K-means的用戶畫像聚類模型

    詳解基于K-means的用戶畫像聚類模型

    這篇文章主要介紹了基于K-means的用戶畫像聚類模型,本文中就是使用one-hot思想將不同維度的數(shù)據(jù)利用字典映射的方式將其轉(zhuǎn)化為數(shù)據(jù)向量,需要的朋友可以參考下
    2022-05-05
  • python matplotlib實(shí)現(xiàn)將圖例放在圖外

    python matplotlib實(shí)現(xiàn)將圖例放在圖外

    這篇文章主要介紹了python matplotlib實(shí)現(xiàn)將圖例放在圖外,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python服務(wù)器與android客戶端socket通信實(shí)例

    python服務(wù)器與android客戶端socket通信實(shí)例

    這篇文章主要介紹了python服務(wù)器與android客戶端socket通信的實(shí)現(xiàn)方法,實(shí)例形式詳細(xì)講述了Python的服務(wù)器端實(shí)現(xiàn)原理與方法,以及對應(yīng)的Android客戶端實(shí)現(xiàn)方法,需要的朋友可以參考下
    2014-11-11

最新評論

山阴县| 游戏| 横峰县| 都江堰市| 祁连县| 南漳县| 大田县| 邳州市| 乌鲁木齐市| 于都县| 商洛市| 石台县| 黄龙县| 兴和县| 莱西市| 织金县| 南皮县| 克东县| 固原市| 吉林省| 萍乡市| 耒阳市| 博白县| 基隆市| 鄂伦春自治旗| 辽源市| 宜丰县| 盐池县| 海门市| 海宁市| 滁州市| 白山市| 环江| 庆安县| 通河县| 军事| 盘锦市| 三都| 江达县| 岳阳市| 磐安县|