Created
June 6, 2013 04:22
-
-
Save vgmoose/5719298 to your computer and use it in GitHub Desktop.
sudoku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
matrix = [[0 for i in range(9)] for j in range(9)] | |
#m = [list("003020600"),list("900305001"),list("001806400"),list("008102900"),list("700000008"),list("006708200"),list("002609500"),list("800203009"),list("005010300")] | |
# | |
#for x in range(0, len(matrix)): | |
# for y in range(0,len(matrix[0])): | |
# matrix[x][y] = int(m[x][y]) | |
def draw_board(): | |
for x in range(0, len(matrix)): | |
if (x%3 == 0): print(" ------ ------ ------") | |
for y in range(0,len(matrix[0])): | |
if (y%3 == 0): sys.stdout.write("|") | |
if (matrix[x][y] == 0 or type(matrix[x][y]) is set): | |
sys.stdout.write(" ") | |
else: | |
sys.stdout.write(""+str(matrix[x][y])+" ") | |
print "" | |
print "\n\n" | |
def getRCPoss(y, b): | |
listy = [] | |
for n in range(1, 10): | |
found = False | |
for c in range(0, 9): | |
if (b and (matrix[c][y] == n)): | |
found = True | |
if (not b and (matrix[y][c] == n)): | |
found = True | |
if (not found): | |
listy.append(n) | |
return listy | |
def getBlockPoss(x, y): | |
listy = [] | |
supx = x/3 | |
supy = y/3 | |
for n in range(1, 10): | |
found = False | |
for a in range(supx*3, supx*3+3): | |
for b in range(supy*3, supy*3+3): | |
if (matrix[a][b] == n): | |
found = True | |
if (not found): | |
listy.append(n) | |
return listy | |
while True: | |
var = raw_input().split(" ") | |
if var == ['0', '0', '0']: | |
break | |
matrix[int(var[0])-1][int(var[1])-1] = int(var[2]) | |
draw_board() | |
while True: | |
z_count = 0 | |
for x in range(0, len(matrix)): | |
for y in range(0,len(matrix[0])): | |
if (matrix[x][y] == 0 or type(matrix[x][y]) is set): | |
mset = set(getRCPoss(y, True))&set(getRCPoss(x, False))&set(getBlockPoss(x, y)) | |
# print mset | |
if (len(mset) == 1): | |
for a in mset: | |
matrix[x][y] = a | |
else: | |
matrix[x][y] = mset | |
z_count+=1 | |
draw_board() | |
if (z_count == 0): | |
break | |
draw_board() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment