zoukankan      html  css  js  c++  java
  • leetcode python 037 求解数独

    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)

    ----蚂蚁不在线
  • 相关阅读:
    经典哦,男女3.8,8.3
    在web窗体设计器中未能加载该文件
    使用客户端脚本
    C#的数据类型
    实验下cookie
    C#中Split分隔字符串的应用
    未将对象引用设置到对象的实例
    System.StackOverflowException 的异常;jit调试失败
    DataGrid中添加删除确认对话框 多种实现
    常用正则表达式
  • 原文地址:https://www.cnblogs.com/offline-ant/p/9535804.html
Copyright © 2011-2022 走看看