Sudoku Game algorithm

Source: Internet
Author: User

#

#author: Wuhao

#

#解数独游戏

#

#想法: From the Beginning (0,0) down, (of course, from where to start the traversal is irrelevant, but the code may be a bit of a change in the wording, I started from (0,0) to the level of traversal), first determine whether it needs to fill the check (a,i,j), If you need to fill in a value to fill in its possible values in Filldata (A,I,J), the return is a list of its possible values, and then iterate to find the correct result

Check function:

def Check (a,i,j):     if return True     return False

Filldata function

defFilldata (a,i,j):ifCheck (A,I,J):#If a data fill is requiredlist_a=[1,2,3,4,5,6,7,8,9]         forKinchRange (9):            ifA[i][k]!=0:#longitudinal judgment, if already overflowed, pass off                Try: List_a.remove (a[i][k])except:Pass            ifA[k][j]!=0:#horizontal judgment, if it has been removed, pass off                Try: List_a.remove (a[k][j])except:Pass         forKinchRange (3):            ifA[int (I/3) *3+k][int (J/3) *3]!=0:Try: List_a.remove (a[int (i/3) *3+k][int (J/3)])                except:Pass            ifA[int (I/3) *3+k][int (J/3) *3+1]!=0:Try: List_a.remove (a[int (i/3) *3+k][int (J/3) *3+1])                except:Pass            ifA[int (I/3) *3+k][int (J/3) *3+2]!=0:Try: List_a.remove (a[int (i/3) *3+k][int (J/3) *3+2])                except:Pass        returnlist_areturnA[I][J]
View Code

The most important traversal lookup result function listData

Game_over=false #初始化未找到结果, locate the result settings game_over=true

defListData (a,posx,posy,f):GlobalGame_over#Game_over is a global variable rather than a local    ifGame_over==true:returnaifPosx! = 8orPosy! = 8:        ifCheck (a,posx,posy): Data=filldata (a,posx,posy) Length=Len (Filldata (a,posx,posy)) forKinchRange (length):#Print ("(%s,%s), gameover=%s"% (Posx,posy,game_over)) is used to see if the test results are wrong                ifgame_over==False:a[posx][posy]=Data[k]#F.write (str (a))                    #f.write ("\ r \ n")ListData (a,posx+ (posy+1)//9, (posy+1)%9, F)Else:returnaifgame_over==False:a[posx][posy]=0Else: ListData (a,posx+ (posy+1)//9, (posy+1)%9, F)Else: Game_over=True A[posx][posy]=Filldata (A,posx,posy) [0]returnA

Main program

if __name__=="__main__": List_a=    [        [8, 0,0,0,0,0,0,0,0], [0,0,3,6, 0,0,0,0,0], [0,7,0,0,9,0,2, 0,0], [0,5,0,0,0,7, 0,0,0], [0,0,0,0,4,5,7, 0,0], [0,0,0,1,0,0,0,3, 0], [0,0,1,0,0,0,0,6,8], [0,0,8,5,0,0,0,1, 0], [0,9,0,0,0,0,4, 0,0]] F= Open ("Log.txt","W")    ifListData (list_a,0,0,f) = =list_a:Print("There is no solution to this sudoku.")    Else:Print(list_a) f.close ()

Results:

Sudoku Game algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.