TI/Skrypty z zajęć/7 sudoku

Z Brain-wiki
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 15 11:16:40 2020

@author: Tomek
"""

p = np.array([[0,0,6,0,3,0,7,0,8],
              [0,3,0,0,0,0,0,0,1],
              [2,0,0,0,0,0,6,0,0],
              [1,0,0,3,5,0,0,0,6],
              [0,7,9,0,4,0,1,5,0],
              [5,0,0,0,1,7,0,0,4],
              [0,0,2,0,0,0,0,0,7],
              [6,0,0,0,0,0,0,8,0],
              [4,0,7,0,6,0,2,0,0]])



def rysuj(t):
    for i in range(9):
        tab=t[i]
        print(tab[0],tab[1],tab[2],'|',tab[3],tab[4],tab[5],'|',tab[6],tab[7],tab[8])
        if (i==2) or (i==5):
            print('------+-------+------')
    print()
            
def Sudoku_tester(tab,x,y,n):
    if tab[x,y]!=0: return False
    if (n in tab[x,:]): return False
    if (n in tab[:,y]): return False
    if (n in tab[3*(x//3):3*(x//3)+3,3*(y//3):3*(y//3)+3]): return False
    return True

def sudoku(tab):
    for x in range(9):
        for y in range(9):
            if tab[x,y]==0:
                for n in range(1,10):
                    if Sudoku_tester(tab,x,y,n):
                        tab[x,y]=n
                        sudoku(tab)
                        tab[x,y]=0
                return
    rysuj(tab)
    return

sudoku(p)

#rysuj(p)
#for n in range(1,10):
#    print(n, Sudoku_tester(p,3,5,n))
    
#def silnia(n):
#    if n==0: return 1
#    return n*silnia(n-1)
#
#print(silnia(200))