richtige Python Datei eingefügt

This commit is contained in:
klaas 2024-09-24 16:12:36 +02:00
parent 438e1c1252
commit edb3e0c86f
1 changed files with 69 additions and 0 deletions

69
sudoku.py Normal file
View File

@ -0,0 +1,69 @@
import time
# Das Sudoku-Startfeld
start = [
[0, 8, 0, 0, 2, 0, 5, 6, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 7],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 5, 0, 0, 9, 0, 4, 0, 8],
[0, 0, 7, 8, 0, 0, 0, 0, 3],
[0, 9, 0, 0, 1, 0, 0, 5, 0],
[2, 0, 4, 0, 0, 0, 8, 0, 0],
[0, 6, 0, 0, 8, 5, 0, 0, 0],
[0, 0, 0, 2, 0, 0, 1, 0, 0]
]
def isfine(feld, x, y):
# Doppelte Zahl in Zeile oder Spalte prüfen
for yi in range(9):
if yi != y and feld[x][yi] == feld[x][y]:
return False
for xi in range(9):
if xi != x and feld[xi][y] == feld[x][y]:
return False
# Neuner-Kästchen-Test
x1 = (x // 3) * 3
y1 = (y // 3) * 3
for xk in range(x1, x1 + 3):
for yk in range(y1, y1 + 3):
if (xk != x or yk != y) and feld[xk][yk] == feld[x][y]:
return False
return True
def nextone(feld, x, y):
if y == 9:
y = 0
x += 1
if x == 9:
return True
if feld[x][y] > 0:
if not isfine(feld, x, y):
return False
return nextone(feld, x, y + 1)
else:
for feld[x][y] in range(1, 10):
if not isfine(feld, x, y):
continue
if nextone(feld, x, y + 1):
return True
feld[x][y] = 0
return False
def main():
tstart = time.time()
if nextone(start, 0, 0):
for row in start:
print(row)
else:
print("Nope!")
time1 = time.time()
print(f"{tstart} - {time1} * {time.CLOCKS_PER_SEC}")
if __name__ == "__main__":
main()