This arctical'll also is published in 中文版 at http://www.cnblogs.com/LarryAtCNBlog/p/4307965.html
Sudoku is a crossword puzzle, widely known is the 9x9 type, the following is an algorithm, that is, each time from the number of can be determined to calculate the possible values of the peer or the same column, if the possible value is only 1, then you can directly determine the value of the cell, if the number of possible values is greater than or equal to 2, then the hypothesis of looping and cell values Calculate the answer. Because the Sudoku answer is not unique, a parameter is added to define the number of answers returned.
param( #return a few answers[INT]$HowManyAnswersYouWanttoGet= 1)$SudokuMatrix= @( @ (0,0,0, 0,0,0, 0,0,3), @ (0,0,0, 0,0,0, 0,4,0), @ (0,5,1, 6,0,0, 0,0,0), @ (0,3,0, 0,0,8, 0,0,2), @ (9,0,0, 1,6,0, 0,0,0), @ (0,6,0, 0,5,4, 0,0,0), @ (5,4,0, 0,0,0, 0,2,0), @ (0,0,3, 4,0,2, 0,0,0), @ (0,0,8, 3,0,0, 7,1,0))#loop through each element, adding an array of 1-9 to the element that has no value. for($i= 0;$i -lt9;$i++){ for($j= 0;$j -lt9;$j++){ if(!$SudokuMatrix[$i][$j]){ $SudokuMatrix[$i][$j] = 1..9 }Else{ $SudokuMatrix[$i][$j] = @($SudokuMatrix[$i][$j]) } }}#loop each element, comparing horizontal and vertical data, from removing all possible values. functionGoloop ($arr){ $NEWARR= @($null) * 9 for($i= 0;$i -lt9;$i++){ $NEWARR[$i] =$arr[$i]. Psobject.copy ()} for($i= 0;$i -lt9;$i++){ for($j= 0;$j -lt9;$j++){ if($NEWARR[$i][$j]. Count-ne1){ for($k= 0;$k -lt9;$k++){ if($NEWARR[$i][$k]. Count-eq1-and $NEWARR[$i][$j]. Count-ne1){ $NEWARR[$i][$j] = @($NEWARR[$i][$j] | ? {$_ -ne $NEWARR[$i][$k][0]}) } if($NEWARR[$k][$j]. Count-eq1-and $NEWARR[$i][$j]. Count-ne1){ $NEWARR[$i][$j] = @($NEWARR[$i][$j] | ? {$_ -ne $NEWARR[$k][$j][0]}) } } } } } return $NEWARR}#looping through each element, if the number of possible values becomes 0, returns True, which indicates a calculation error. functionVerifyzero ($arr){ for($i= 0;$i -lt9;$i++){ for($j= 0;$j -lt9;$j++){ if($arr[$i][$j]. Count-eq0){ return $i,$j,$true } } } return $i,$j,$false}#Find the one that has the smallest number of possible values in the element, and return the position of the element. functionFindsmallest ($arr){ foreach($k inch2..9){ for($i= 0;$i -lt9;$i++){ for($j= 0;$j -lt9;$j++){ if($arr[$i][$j]. Count-eq $k){ return $i,$j,$k } } } }}#calculates how many elements in the array have already been confirmed values. functionCountconfirmednumber ($arr){ $NumberConfirmed= 0 for($i= 0;$i -lt9;$i++){ for($j= 0;$j -lt9;$j++){ if($arr[$i][$j]. Count-eq1){ $NumberConfirmed++ } } } return $NumberConfirmed}$AnswerCount= 0$Results= @()functionGocalculate ($arr){ $NewArray= Goloop ($arr) #Verify no zero option! $ZeroPosition= Verifyzero ($NewArray) if($ZeroPosition[2]){ #write-host "0 option found: [$ ($ZeroPosition [0])][$ ($ZeroPosition [1])]" return } #Confirm Current Numbers if((Countconfirmednumber ($NewArray))-eq81){ $Script: Answercount++Write-host"An answer captured, ID: $AnswerCount"-Foregroundcolor Green#write-host "Bayi numbers confirmed." $Script: Results+=$null $Script: Results[-1] =$NewArray return } #Find the nearest (to [0][0]) and smallest (2 to 9) option element. $Smallest= Findsmallest ($NewArray) $OptionsStack= @($NewArray[$Smallest[0]] [$Smallest[1]]) #write-host "Row: $ ($Smallest [0]); Col: $ ($Smallest [1]); Option: $ ($OptionsStack-join ') " foreach($Option inch $OptionsStack){ #write-host "Set [$ ($Smallest [0])][$ ($Smallest [1])] to: $Option" $NewArray[$Smallest[0]] [$Smallest[1]] = @ ($Option) if($AnswerCount -lt $HowManyAnswersYouWanttoGet) {gocalculate ($NewArray) } }}#triggeringGocalculate ($SudokuMatrix)#Output Results$Results| %{ if($_ -eq $null){return} Write-host"Answer:"-Foregroundcolor Yellow for($i= 0;$i -lt9;$i++){ for($j= 0;$j -lt9;$j++) {Write-host"$ ($_[$i [$j][0])"-nonewline-Foregroundcolor Yellow} Write-host"' n" }}
[PowerShell] calculates 9x9 Sudoku with PowerShell.