2013-09-29 3 views
-1

저는이 프로그램을 비트 단위로 작성하여 각 파트가 계속 수행되는지 테스트했습니다. 그러나, 내가 끝내고, 모든 것을 모으면, 나는 해결책을 얻을 수 없었다. 나는 각 스퀘어에 가능한 숫자의 목록을 만들고 스퀘어를 기존 스퀘어를 기반으로 제거함으로써 스도쿠를 풀려고했다. 사각형에 가능한 숫자가 하나 밖에없는 경우 해결책이라고 생각했습니다. 그리고 그것이 완료 될 때까지 반복 될 것입니다.Python sudoku solver가 솔루션을 반환하지 않습니다.

나는 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 
+0

Stackoverflow는 코드 리뷰를 수행하지 않습니다. –

+0

@ user2799617 : 이것은 디버그 세션만큼 코드 리뷰가 아닙니다. –

+0

@ user2799617 : OP가 문제를 더 쉽게 재현 할 수 있도록 해 줄 수는 없다는 말은 아닙니다. 이 질문은 매우 모호합니다. –

답변

1

당신에게 결코 전화done(). 함수 객체가 True에 결코 동일한 경우에만 테스트 :

while done != True: 

기능 객체가 True 동일한 결코 없다. 단지 전화, 여기에 평등에 대한 기능 테스트하지 마십시오 당신에게 elim_row()명확 수있는 값을

while not done(): 

다음, 당신은 값을 통해 루프를 제거하기 위해 때마다 :

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 

possible[(y,x)]을 설정 즉, 의 빈 값으로 0이 아닌 행의 값이입니다. 다른 2 elim_ 함수에서도 동일하게 수행합니다.

for stuff in get_rid_of: 
    if stuff in possible[(y,x)]: 
     possible[(y,x)].remove(stuff) 

이 취소됩니다 가능성을 정말 빨리 :

당신은 아마 사용하고 싶었다.

관련 문제