码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode python 037 求解数独

时间:2018-08-26 00:06:33      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:port   list   int   code   sha   get   def   shape   limit   

import numpy as np
import sys
sys.setrecursionlimit(1000) #例如这里设置为一百万

def get1(n):
    if n<3:
        return 0
    if n<6:
        return 3
    return 6

def get2(n):
    if n<3:
        return 3
    if n<6:
        return 6
    return 9

def get3(arr,i,j):
    a1=list(arr[i,:])
    a2=list(arr[:,j])
    a3=list(arr[get1(i):get2(i),get1(j):get2(j)].reshape(1,-1)[0])
    a=set(a1+a2+a3)
    return b-a

def sd(arr,dep):
    count[0]+=1
    if dep==len(l):
        result.append(arr)
    else:
        x,y=l[dep]
        lac=get3(arr,x,y)
        if len(lac)>0 and len(result)==0:        
            for i in lac:
                ac=arr.copy()
                ac[x,y]=i
                sd(ac,dep+1)
    
    
array= [[5,3,0,0,7,0,0,0,0],
        [6,0,0,1,9,5,0,0,0],
        [0,9,8,0,0,0,0,6,0],
        [8,0,0,0,6,0,0,0,3],
        [4,0,0,8,0,3,0,0,1],
        [7,0,0,0,2,0,0,0,6],
        [0,6,0,0,0,0,2,8,0],
        [0,0,0,4,1,9,0,0,5],
        [0,0,0,0,8,0,0,7,9]]
array=np.array(array)
b,l,old,result,count={1,2,3,4,5,6,7,8,9},[],[],[],[0]
for i in range(0,9):
    for j in range(0,9):
        if array[i,j]==0:
            l.append((i,j))
sd(array,0)
print(result)

leetcode python 037 求解数独

标签:port   list   int   code   sha   get   def   shape   limit   

原文地址:https://www.cnblogs.com/offline-ant/p/9535804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!