私はこのプログラムを少しずつ書き、各部分が機能することをテストしてから先に進みました。しかし、完成してすべてをまとめてみると、解決策が得られませんでした。各正方形の可能な数字のリストを作成し、既存の正方形に基づいてそれらを削除することにより、数独を解決しようとしていました。正方形に可能な数が1つしかない場合、それが解になると思いました。そして、終了するまでループします。
私は自分のコードを30分調べましたが、まだ運がありません。raw_input("")
問題がないか確認するために挿入しました。最初はある程度の進歩が見られましたが、その後停止しました。
そこで、座標の可能な数字を印刷し、プロセスのどこかで、すべての可能性を削除しました。
コードは次のようになります。
# Create the Sodoku grid
grid = [[3,2,0,1,6,0,8,0,9],
[0,7,8,9,0,3,1,2,6],
[6,0,0,8,0,0,4,5,3],
[7,1,0,4,0,0,0,6,2],
[5,4,0,0,0,0,0,0,7],
[0,0,0,2,0,5,3,1,0],
[0,5,9,7,4,0,2,0,8],
[2,0,7,5,0,9,0,0,0],
[8,6,4,0,0,0,0,9,5],]
# Create possibilities
possible = {}
for y in range(9):
for x in range(9):
possible[(y,x)] = [1,2,3,4,5,6,7,8,9]
# A function that returns the row it is in.
def check_row(y,x):
return grid[y]
# A function that returns the column it is in.
def check_column(y,x):
column = []
for hops in range(9):
column.append(grid[hops][x])
return column
# A function that returns the square it is in.
# -------------
# 1| 0 | 1 | 2 |
# -------------
# 2| 3 | 4 | 5 |
# -------------
# 3| 6 | 7 | 8 |
# -------------
# 1 2 3
def check_square(they,thex):
square0 = []
square1 = []
square2 = []
square3 = []
square4 = []
square5 = []
square6 = []
square7 = []
square8 = []
for y in range(3):
for x in range(3):
square0.append([y,x])
for y in range(3):
for x in range(3,6):
square1.append([y,x])
for y in range(3):
for x in range(6,9):
square2.append([y,x])
for y in range(3,6):
for x in range(3):
square3.append([y,x])
for y in range(3,6):
for x in range(3,6):
square4.append([y,x])
for y in range(3,6):
for x in range(6,9):
square5.append([y,x])
for y in range(6,9):
for x in range(3):
square6.append([y,x])
for y in range(6,9):
for x in range(3,6):
square7.append([y,x])
for y in range(6,9):
for x in range(6,9):
square8.append([y,x])
tests = [square0,
square1,
square2,
square3,
square4,
square5,
square6,
square7,
square8]
square_list = []
def list_of_grid(result):
for cood in result:
[they,thex] = cood
square_list.append(grid[they][thex])
# Check which square it of and print the list of grid
for test in tests:
if [they,thex] in test:
list_of_grid(test)
return square_list
# Function that eliminates row possibilities
def elim_row(y, x):
get_rid_of = []
for element in check_row(y, x):
if element != 0:
get_rid_of.append(element)
for stuff in get_rid_of:
try:
if stuff in possible[(y,x)]:
possible[(y,x)] = []
else:
possible[(y,x)].remove(stuff)
except ValueError:
pass
# Funciton that eliminates column possibilites
def elim_column(y, x):
get_rid_of = []
for element in check_column(y, x):
if element != 0:
get_rid_of.append(element)
for stuff in get_rid_of:
try:
if stuff in possible[(y,x)]:
possible[(y,x)] = []
else:
possible[(y,x)].remove(stuff)
except ValueError:
pass
# Function that eliminates square possibilites
def elim_square(y, x):
get_rid_of = []
for element in check_square(y, x):
if element != 0:
get_rid_of.append(element)
for stuff in get_rid_of:
try:
if stuff in possible[(y,x)]:
possible[(y,x)] = []
else:
possible[(y,x)].remove(stuff)
except ValueError:
pass
# Check if done:
def done():
empty = 0
for y in range(9):
for x in range(9):
if grid[y][x] == 0:
empty += 1
if empty == 0:
return True
else:
return False
# print grid
if __name__ == "__main__":
# Go through each row, column and square and delete possibilites
while done != True:
raw_input("")
for cood in possible.keys():
(y, x) = cood
elim_row(y,x)
elim_column(y,x)
elim_square(y,x)
# Check if len of possible == 1
if len(possible[cood]) == 1:
grid[y][x] = possible[cood][0]
print possible[(0,2)]
for rows in grid:
print rows