数独的大量变量的蛮力法

时间:2019-07-05 16:31:39

标签: python methods sudoku

基本上我的问题是:如何暴力数独?

现在,我创建了一个数独,确定了每个单元中的可能性,并找到了一种检查不同功能结果的方法。尚不起作用的唯一功能是蛮力功能。现在,它仅返回可能性列表。

我当然可以编写81个循环并找到答案,但是我正在寻找一种更好的方法。有谁知道吗?

from numpy import *

def create():
    return array([[0,8,5 , 0,7,0 , 4,0,0],
                  [0,0,0 , 0,0,0 , 0,0,6],
                  [1,0,0 , 0,0,9 , 2,0,8],

                  [0,9,0 , 0,1,0 , 0,0,4],
                  [4,0,3 , 0,6,0 , 5,0,1],
                  [2,0,0 , 0,4,0 , 0,3,0],

                  [3,0,7 , 8,0,0 , 0,0,2],
                  [5,0,0 , 0,0,0 , 0,0,0],
                  [0,0,4 , 0,9,0 , 3,6,0]])

def display(sudoku):
    print('')
    for i in range(9):
        for j in range(9):
            if j==8:
                if sudoku[i,j]>0: print(sudoku[i,j])
                else: print('_')
            elif j==2:
                if sudoku[i,j]>0: print(sudoku[i,j],end="   ")
                else: print('_',end="   ")
            elif j==5:
                if sudoku[i,j]>0: print(sudoku[i,j],end="   ")
                else: print('_',end="   ")                         
            else:
                if sudoku[i,j]>0: print(sudoku[i,j],end=" ")
                else: print('_',end=" ")
        if i==2: print('')
        if i==5: print('')
    print('')

def possibilities(r,c):
    ret=[]
    if sudoku[r,c]>0:ret.append(sudoku[r,c]);return ret

    def row(n):
        lis=[]
        for i in range(9):
            if sudoku[n,i]>0:
                lis.append(sudoku[n,i])
        return lis

    def column(n):
        lis=[]
        for i in range(9):
            if sudoku[i,n]>0:
                lis.append(sudoku[i,n])
        return lis

    def box(r,c):
        if r<=2: rvar=array([0,1,2])
        elif r<=5: rvar=array([3,4,5])            
        else: rvar=array([6,7,8])             
        if c<=2: cvar=array([0,1,2])
        elif c<=5: cvar=array([3,4,5])            
        else: cvar=array([6,7,8])         
        b=[]
        for i in rvar:
            for j in cvar:
                if sudoku[i,j]>0:
                    b.append(sudoku[i,j])
        return b

    used=[]
    for i in row(r):
        used.append(i)
    for i in column(c):
        used.append(i)
    for i in box(r,c):
        used.append(i)

    count=zeros(10)
    for i in range(1,10):
        for j in used:
            if i==j: count[i]+=1


    for i in range(1,10):
        if count[i]==0: ret.append(i)

    return ret

def check(sudoku):
    for i in range(9):
        val=0
        for j in range(9):
            val+=10**sudoku[i,j]
        if val!=1111111110: return 0
    for i in range(9):
        val=0
        for j in range(9):
            val+=10**sudoku[j,i]
        if val!=1111111110: return 0
    for j in range(9):
        val=0
        if (j==0 or j==3 or j==6): rvar=array([0,1,2])
        elif (j==1 or j==4 or j==7): rvar=array([3,4,5])
        else: rvar=array([6,7,8])             
        if j<=2: cvar=array([0,1,2])
        elif j<=5: cvar=array([3,4,5])            
        else: cvar=array([6,7,8])
        for k in range(3):
            for l in range(3):
                val+=10**sudoku[rvar[l],cvar[k]]
        if val!=1111111110: 
            return 0
    return 1

def bruteforce(sudoku):
    p=[]
    for i in range(9):
        for j in range(9):
            p.append(possibilities(i,j))    
    return p 

sudoku=create()
display(sudoku)
bruteforce(sudoku)

0 个答案:

没有答案