还踢球?吃鸡啦!PQ 解数独问题

首先将以下代码放置于本地python脚本文件中,比如代码写在我的f:\sd.py文件中(请启用搬运工大法):

class point:  
    def __init__(self,x,y):  
        self.x=x  
        self.y=y  
        self.available=[]  
        self.value=0  

def rowNum(p,sudoku):  
    row=set(sudoku[p.y*9:(p.y+1)*9])  
    row.remove(0)  
    return row #set type  

def colNum(p,sudoku):  
    col=[]  
    length=len(sudoku)  
    for i in range(p.x,length,9):  
        col.append(sudoku[i])  
    col=set(col)  
    col.remove(0)  
    return col #set type  

def blockNum(p,sudoku):  
    block_x=p.x//3  
    block_y=p.y//3  
    block=[]  
    start=block_y*3*9+block_x*3  
    for i in range(start,start+3):  
        block.append(sudoku[i])  
    for i in range(start+9,start+9+3):  
        block.append(sudoku[i])  
    for i in range(start+9+9,start+9+9+3):  
        block.append(sudoku[i])  
    block=set(block)  
    block.remove(0)  
    return block #set type  

def initPoint(sudoku):  
    pointList=[]  
    length=len(sudoku)  
    for i in range(length):  
        if sudoku[i]==0:  
            p=point(i%9,i//9)  
            for j in range(1,10):  
                if j not in rowNum(p,sudoku) and j not in colNum(p,sudoku) and j not in blockNum(p,sudoku):  
                    p.available.append(j)  
            pointList.append(p)  
    return pointList  

def tryInsert(p,sudoku):  
    availNum=p.available  
    for v in availNum:  
        p.value=v  
        if check(p,sudoku):  
            sudoku[p.y*9+p.x]=p.value  
            if len(pointList)<=0:
                showSudoku(sudoku)  
                exit()  
            p2=pointList.pop()  
            tryInsert(p2,sudoku)  
            sudoku[p2.y*9+p2.x]=0  
            sudoku[p.y*9+p.x]=0  
            p2.value=0  
            pointList.append(p2)  
        else:  
            pass      

def check(p,sudoku):  
    if p.value==0:  
        print('not assign value to point p!!')  
        return False  
    if p.value not in rowNum(p,sudoku) and p.value not in colNum(p,sudoku) and p.value not in blockNum(p,sudoku):  
        return True  
    else:  
        return False  

def showSudoku(sudoku):  
    for j in range(9):  
        for i in range(9):  
            print('%d '%(sudoku[j*9+i]),end='')  
        print('')     
from sys import argv
a,b=argv
if __name__=='__main__':  
    sudoku=list(map(int,list(b)))

    pointList=initPoint(sudoku)   
    p=pointList.pop()  
    print(tryInsert(p,sudoku))

然后pbi中做个自定义函数:

let
fx=(x)=>Python.Execute("p=os.popen('python f:/sd.py "&x&"')
sd=pandas.DataFrame([p.read()])"){0}[Value][0]{0}
in
fx

将这个函数定义过程起名叫"数独"
然后用这个函数解数独了:
file
注意:我将数独中空位用0代表,9行9列一共81个数字按照一行接一行的写法顺序书写!
我是搬运工,我是搬运工,我是搬运工....
file

道高一尺 魔高一丈
https://pbihub.cn/users/44
M与DAX的恩怨纠葛