Python数独求解器很好的布局

时间:2014-03-13 19:36:28

标签: python layout sudoku

也许你已经看过我关于我正在写的数独求解器的另一个问题。现在我很高兴他能做到这一点。但是当我运行它时,它看起来不像数独。 我试图让它看起来更像一个数独但我不能比这更进一步:

* 2 9 4 8 6 * 1 3 
1 3 * 7 9 * 4 6 * 
* 6 4 * 3 1 7 9 2 
3 9 1 6 4 7 2 * 5 
4 5 * 2 1 3 * 7 6 
* 7 2 9 * 8 * 4 1 
9 * 7 8 2 * 6 3 * 
5 8 6 * 7 4 1 2 9 
2 4 3 1 6 * 8 5 7

这是产生这个答案的代码:

def print_sudoku(array):
    for i in range(0,9):
        print()
        for j in range(0,9):
            print(array[i][j][0],end=' ')

import pprint
distance = [[[0 for k in range(10)] for j in range(9)] for i in range(9)]

distance[0][0][0] = '*'
distance[0][1][0] = 2
distance[0][2][0] = 9
distance[0][3][0] = 4
distance[0][4][0] = 8
distance[0][5][0] = 6
distance[0][6][0] = '*'
distance[0][7][0] = 1
distance[0][8][0] = 3
distance[1][0][0] = 1
distance[1][1][0] = 3
distance[1][2][0] = '*'
distance[1][3][0] = 7
distance[1][4][0] = 9
distance[1][5][0] = '*'
distance[1][6][0] = 4
distance[1][7][0] = 6
distance[1][8][0] = '*'
distance[2][0][0] = '*'
distance[2][1][0] = 6
distance[2][2][0] = 4
distance[2][3][0] = '*'
distance[2][4][0] = 3
distance[2][5][0] = 1
distance[2][6][0] = 7
distance[2][7][0] = 9
distance[2][8][0] = 2
distance[3][0][0] = 3
distance[3][1][0] = 9
distance[3][2][0] = 1
distance[3][3][0] = 6
distance[3][4][0] = 4
distance[3][5][0] = 7
distance[3][6][0] = 2
distance[3][7][0] = '*'
distance[3][8][0] = 5
distance[4][0][0] = 4
distance[4][1][0] = 5
distance[4][2][0] = '*'
distance[4][3][0] = 2
distance[4][4][0] = 1
distance[4][5][0] = 3
distance[4][6][0] = '*'
distance[4][7][0] = 7
distance[4][8][0] = 6
distance[5][0][0] = '*'
distance[5][1][0] = 7
distance[5][2][0] = 2
distance[5][3][0] = 9
distance[5][4][0] = '*'
distance[5][5][0] = 8
distance[5][6][0] = '*'
distance[5][7][0] = 4
distance[5][8][0] = 1
distance[6][0][0] = 9
distance[6][1][0] = '*'
distance[6][2][0] = 7
distance[6][3][0] = 8
distance[6][4][0] = 2
distance[6][5][0] = '*'
distance[6][6][0] = 6
distance[6][7][0] = 3
distance[6][8][0] = '*'
distance[7][0][0] = 5
distance[7][1][0] = 8
distance[7][2][0] = 6
distance[7][3][0] = '*'
distance[7][4][0] = 7
distance[7][5][0] = 4
distance[7][6][0] = 1
distance[7][7][0] = 2
distance[7][8][0] = 9
distance[8][0][0] = 2
distance[8][1][0] = 4
distance[8][2][0] = 3
distance[8][3][0] = 1
distance[8][4][0] = 6
distance[8][5][0] = '*'
distance[8][6][0] = 8
distance[8][7][0] = 5
distance[8][8][0] = 7

print_sudoku(distance)

我唯一想要的是它看起来更像数独 谢谢 鲁迪

2 个答案:

答案 0 :(得分:1)

def print_sudoku(array):
    for i in range(10):
        print()
        if i%3 == 0:
            print("-" * 19)
            if i == 9:
                break
        print ("|", end='')
        for j in range(9):
            print(array[i][j][0], end=' ' if j % 3 != 2 else '|')

如果你想要颜色,你需要指定你正在使用的终端

答案 1 :(得分:0)

这是我用来打印数独的功能

def PrintSudoku(board):
    y = 0
    for n in board:
        x = 0
        while x != 3:
            print n[x],
            x += 1
        print "|",
        while x != 6:
            print n[x],
            x += 1
        print "|",
        while x != 9:
            print n[x],
            x += 1
        print ""
        y = y + 1
        if y == 3 or y == 6:
            print "------|-------|------"

如果你将数独格式化为数组中的数组,就像这样

Sudoku = [[0,2,9,4,8,6,0,1,3],
          [1,3,0,7,9,0,4,6,0],
          [0,6,4,0,3,1,7,9,2],
          [3,9,1,6,4,7,2,0,5],
          [4,5,0,2,1,3,0,7,6],
          [0,7,2,9,0,8,0,4,1],
          [9,0,7,8,2,0,6,3,0],
          [5,8,6,0,7,4,1,2,9],
          [2,4,3,1,6,0,8,5,7]]

它看起来应该是这样的

0 2 9 | 4 8 6 | 0 1 3 
1 3 0 | 7 9 0 | 4 6 0 
0 6 4 | 0 3 1 | 7 9 2 
------|-------|------
3 9 1 | 6 4 7 | 2 0 5 
4 5 0 | 2 1 3 | 0 7 6 
0 7 2 | 9 0 8 | 0 4 1 
------|-------|------
9 0 7 | 8 2 0 | 6 3 0 
5 8 6 | 0 7 4 | 1 2 9 
2 4 3 | 1 6 0 | 8 5 7