完成數獨的算法 python_python實現數獨算法實例

本文實例講述了python實現數獨算法的方法。分享給大家供大家參考。具體如下:

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

'''

Created on 2012-10-5

@author: Administrator

'''

from collections import defaultdict

import itertools

a = [

[ 0, 7, 0, 0, 0, 0, 0, 0, 0], #0

[ 5, 0, 3, 0, 0, 6, 0, 0, 0], #1

[ 0, 6, 2, 0, 8, 0, 7, 0, 0], #2

#

[ 0, 0, 0, 3, 0, 2, 0, 5, 0], #3

[ 0, 0, 4, 0, 1, 0, 3, 0, 0], #4

[ 0, 2, 0, 9, 0, 5, 0, 0, 0], #5

#

[ 0, 0, 1, 0, 3, 0, 5, 9, 0], #6

[ 0, 0, 0, 4, 0, 0, 6, 0, 3], #7

[ 0, 0, 0, 0, 0, 0, 0, 2, 0], #8

# 0, 1, 2, 3,|4, 5, 6,|7, 8

]

#a = [

# [0, 0, 0, 0, 0, 0, 0, 0, 0], #0

# [0, 0, 0, 0, 0, 0, 0, 0, 0], #1

# [0, 0, 0, 0, 0, 0, 0, 0, 0], #2

# #

# [0, 0, 0, 0, 0, 0, 0, 0, 0], #3

# [0, 0, 0, 0, 0, 0, 0, 0, 0], #4

# [0, 0, 0, 0, 0, 0, 0, 0, 0], #5

# #

# [0, 0, 0, 0, 0, 0, 0, 0, 0], #6

# [0, 0, 0, 0, 0, 0, 0, 0, 0], #7

# [0, 0, 0, 0, 0, 0, 0, 0, 0], #8

## 0, 1, 2, 3,|4, 5, 6,|7, 8

# ]

exists_d = dict((((h_idx, y_idx), v) for h_idx, y in enumerate(a) for y_idx , v in enumerate(y) if v))

h_exist = defaultdict(dict)

v_exist = defaultdict(dict)

for k, v in exists_d.items():

h_exist[k[ 0]][k[ 1]] = v

v_exist[k[ 1]][k[ 0]] = v

aa = list(itertools.permutations(range(1, 10), 9))

h_d = {}

for hk, hv in h_exist.items():

x = filter(lambda x:all((x[k] == v for k, v in hv.items())), aa)

x = filter(lambda x:all((x[vk] != v for vk , vv in v_exist.items() for k, v in vv.items() if k != hk)), x)

# print x

h_d[hk] = x

def test(x, y):

return all([y[i] not in [x_[i] for x_ in x] for i in range(len(y)) ])

def test2(x):

return len(set(x)) != 9

s = set(range(9))

sudokus = []

for l0 in h_d[0 ]:

for l1 in h_d[ 1]:

if not test((l0,), l1):

continue

for l2 in h_d[ 2]:

if not test((l0, l1), l2):

continue

# 1,2,3行 進行驗證

if test2([l0[ 0], l0[ 1], l0[ 2]

, l1[ 0], l1[ 1], l1[ 2]

, l2[ 0], l2[ 1], l2[ 2]

]) : continue

if test2([l0[ 3], l0[ 4], l0[ 5]

, l1[ 3], l1[ 4], l1[ 5]

, l2[ 3], l2[ 4], l2[ 5]

]) : continue

if test2([l0[ 6], l0[ 7], l0[ 8]

, l1[ 6], l1[ 7], l1[ 8]

, l2[ 6], l2[ 7], l2[ 8]

]) : continue

for l3 in h_d[ 3]:

if not test((l0, l1, l2), l3):

continue

for l4 in h_d[ 4]:

if not test((l0, l1, l2, l3), l4):

continue

for l5 in h_d[ 5]:

if not test((l0, l1, l2, l3, l4), l5):

continue

# 4,5,6行 進行驗證

if test2([l3[ 0], l3[ 1], l3[ 2]

, l4[ 0], l4[ 1], l4[ 2]

, l5[ 0], l5[ 1], l5[ 2]

]) : continue

if test2([l3[ 3], l3[ 4], l3[ 5]

, l4[ 3], l4[ 4], l4[ 5]

, l5[ 3], l5[ 4], l5[ 5]

]) : continue

if test2([l3[ 6], l3[ 7], l3[ 8]

, l4[ 6], l4[ 7], l4[ 8]

, l5[ 6], l5[ 7], l5[ 8]

]) : continue

for l6 in h_d[ 6]:

if not test((l0, l1, l2, l3, l4, l5,), l6):

continue

for l7 in h_d[ 7]:

if not test((l0, l1, l2, l3, l4, l5, l6), l7):

continue

for l8 in h_d[ 8]:

if not test((l0, l1, l2, l3, l4, l5, l6, l7), l8):

continue

# 7,8,9行 進行驗證

if test2([l6[ 0], l6[ 1], l6[ 2]

, l7[0 ], l7[1 ], l7[2 ]

, l8[0 ], l8[1 ], l8[2 ]

]) : continue

if test2([l6[ 3], l6[ 4], l6[ 5]

, l7[3 ], l7[4 ], l7[5 ]

, l8[3 ], l8[4 ], l8[5 ]

]) : continue

if test2([l6[ 6], l6[ 7], l6[ 8]

, l7[6 ], l7[7 ], l7[8 ]

, l8[6 ], l8[7 ], l8[8 ]

]) : continue

print l0

print l1

print l2

print l3

print l4

print l5

print l6

print l7

print l8

sudokus.append((l0, l1, l2, l3, l4, l5, l6, l7, l8))

希望本文所述對大家的Python程序設計有所幫助。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/533972.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/533972.shtml
英文地址,請注明出處:http://en.pswp.cn/news/533972.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python讀取多個文件csv_Python:讀取多個文本文件并寫入相應的csv文件

我在別處找不到這個問題的答案,所以我將繼續把它貼在這里:我有一個Python腳本,它將讀取文本文件的內容,將其內容拆分為單詞,然后輸出一個CSV文件,該文件將文本縮減為單詞頻率列表。(最后,我將插…

java treetable_在Swing中創建TreeTable | 學步園

TreeTable是Tree和Table的一個結合-就是一個即能夠展開和收起行,同時也能夠顯示多個列的組件。在Swing的標準包里沒有一個叫做JtreeTable的組件,但是我們很容易通過把Jtree做成Jtable的渲染器來創建一個這樣的組件。這篇文章就是關于如何使用…

python爬去微博十大流行語_用python重新定義【2019十大網絡流行語】-后臺/架構/數據庫-敏捷大拇指-一個敢保留真話的IT精英社區...

↑關注 置頂 ~ 有趣的不像個技術號52568040f9313098ffa367d9d9d21437.jpg (5.73 KB, 下載次數: 0)2019-12-10 04:43 上傳3f4d5fe0016d011a7a68af763314befd.jpg (1.06 KB, 下載次數: 0)2019-12-10 04:43 上傳“文明互鑒真硬核,融梗檸檬誰覺得。霸凌第一九九六&…

java中怎么獲取配置文件的值_java如何獲取配置文件的值

轉:原創 編碼小王子 發布于2018-10-11 18:07:52 閱讀數 2722 收藏展開java大型項目中都會很多系統常量,比如說數據庫的賬號和密碼,以及各種token值等,都需要統一的管理,如果零落的散布到各個類等具體的代碼中的話,在后期管理上將是一場災難,所有需要對這些變量進行統…

python實現隊列_用Python實現的數據結構與算法:隊列

一、概述隊列(Queue)是一種先進先出(FIFO)的線性數據結構,插入操作在隊尾(rear)進行,刪除操作在隊首(front)進行。二、ADT隊列ADT(抽象數據類型)一般提供以下接口:Queue() 創建隊列enqueue(item) 向隊尾插入項dequeue() 返回隊首的項&#xf…

java 監聽窗口是否改變_JAVA項目監聽文件是否發生變化

一.spring容器都初始化完成之后做操作packagecom.bijian.study.listener;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.ApplicationListener;importorg.springframework.context.event.ContextRefreshedEvent;importorg.s…

笨辦法學python3 pdf 腳本之家_解決python3輸入的坑——input()

如下所示:a,b,c,d input()很簡單的代碼,如果輸入為1 -1 -2 3結果會報錯,原因在于input函數會將你的輸入作為python腳本運行,那么輸入就變成了1-1 -2 3,即0 -2 3結果當然是錯誤的了,解決辦法就是將輸入用引…

java 數組寫法_java書寫、數據類型、數組定義

這里只記錄java與php、javascript不同的地方,相同的地方就不贅述了。1.java文件源碼為以.java為后綴的文件,字節碼文件是以.class為后綴的文件。2.寫好一個java源碼之后,cmd進入源碼文件盤符,用命令 javac helloworld.java將源碼轉…

python爬蟲高級知識點_Python爬蟲知識點梳理總結,殿堂級小白入門必讀

數據分析是任何技術一樣。你應該學習的目標。目標就像燈塔,指引你前進。我見過很多合作伙伴學習學習,然后學會放棄。事實上,很大一部分原因是沒有明確的目標,所以你必須清楚學習的目的。你準備學習爬行之前,問問你自己為什么你想學爬行。有些人為了工作,一些為了好玩,和做一定黑…

java running_Running

/****/package test;import java.sql.ResultSet;import java.sql.SQLException;/*** author huangqin**/public class QuestString {private int curPage;//當前頁數private int maxPage;//最大頁數private int maxRowCount;//總記錄數private int pageSize2;//每頁顯示的記錄數…

python停用詞表_多版本中文停用詞詞表 + 多版本英文停用詞詞表 + python詞表合并程序...

文章簡介與更新記錄如果你只想獲取中文停用詞此表,請直接到文章結尾下載項目文件,其中包括三個中文停用詞詞表,一個英文停用詞詞表和一個合并詞表的.py文件2017/07/04 創建文章,上傳文件2017/07/04 更新了合并代碼,添加了新的中文停用詞表(哈工大擴展版本)和一個新的停用詞表,現…

mysql collect_set_hive列轉行 (collect_set())

一、問題hive如何將a b 1a b 2a b 3c d 4c d 5c d 6變為:a b 1,2,3c d 4,5,6二、數據test.txta b 1a b 2a b 3c d …

python編寫遞歸函數和非遞歸函數、輸出斐波那契數列_分別用非遞歸和遞歸的方法編寫函數求斐波那契數列第n項。斐波那契數列1,1,2,3,5,8,13,…...

展開全部/**已知Fibonacci數列&#xff1a;1,1,2,3,5,8,……&#xff0c;F(1)1&#xff0c;F(2)1&#xff0c;F(n)F(n-1)F(n-2)*/#include #include typedef long long int int64;//方法1&#xff0c;遞歸法int64 Fibonacci(int n){int64 sum;if(n<0){printf("參數值e6…

python3.6安裝ipython_centos6.5下安裝python3.6、pip、ipython

一.先更換系統源為了下載順暢一般都會更改為國內源。1 cd /etc/yum.repos.d/2 wget http://mirrors.163.com/.help/CentOS6-Base-163.repo #下載網易源3 mv CentOS-Base.repo CentOS-Base.repo.ori #備份源4 mv CentOS6-Base-163.repo CentOS-Base.repo #把網易源更改為默認源二…

java 多線程的同步問題_java多線程解決同步問題的幾種方式,原理和代碼

wait()/notify()方法await()/signal()方法BlockingQueue阻塞隊列方法PipedInputStream/PipedOutputStream阻塞隊列的一個簡單實現&#xff1a;public class BlockingQueue {private List queue new LinkedList();private int limit 10;public BlockingQueue(int limit){this…

python期末大作業_大一期末考試很重要,考得好不僅有機會有錢拿,還有機會換專業...

現階段很多高校放寒假的時間已經公布&#xff0c;這也就意味著&#xff0c;大學期末考試即將到來。對于大一新生來說&#xff0c;大學的期末考試是比較新鮮的&#xff0c;因為大家都沒有經歷過。經歷過大學考試的學生&#xff0c;都知道大學的大概學習模式&#xff0c;一般情況…

java http 302重定向_Java 純HTTP請求 禁止302自動重定向

Java 純HTTP Get請求獲取響應內容&#xff0c;如果發生302重定向&#xff0c;繼而模擬請求域獲取重定向后的響應內容。關鍵點&#xff1a;設置conn.setInstanceFollowRedirects為false即可示例代碼public static void main(String[] args) {try {StringBuffer buffer new Stri…

python 且_Pyface庫:一個基于pyqt、pyside、wx且簡化的python的GUI

1 說明&#xff1a;1.1 Pyface庫由大名鼎鼎的enthought出品。1.2 介紹&#xff1a;1.2.1 英文&#xff1a;traits-capable windowing framework.The pyface project contains a toolkit-independent GUI abstraction layer, which is used to support the "visualization&…

java方法的參數類型_Java 基礎 14 方法的重載 與 方法參數類型詳解

1.1 方法重載的概述和特點方法重載概述在同一個類中&#xff0c;允許存在一個以上的同名方法&#xff0c;只要它們的參數個數或者參數類型不同即可。方法重載特點與返回值類型無關&#xff0c;只看方法名和參數列表在調用時&#xff0c;虛擬機通過參數列表的不同來區分同名方法…

crv儀表上的i是什么指示燈_汽車打不著火是怎么回事,儀表盤汽車發動機故障燈亮是什么情況故障指示燈圖解大全集...

如果打不著火&#xff0c;那有可能是起動機壞了&#xff0c;有可能是電池沒電了&#xff0c;有可能是電路出現了問題&#xff0c;還有可能是點火系統出現了問題。汽車發動機的點火系統主要部件是火花塞和點火線圈&#xff0c;火花塞是一個需要定期更換的易損件。如果火花塞長時…