[Powershell] An algorithm to 9x9 Sudoku.

Source: Internet
Author: User

Sudoku is a numbers filling game, most known by people are 9x9 class, the algorithm is using confirmed numbers-get all P Ossible numbers for each cell, if the number if possible numbers for a cell was 1, then the cell value confirmed, if Greate R or equal than 2, script makes loop and calculates a potential answer. Since answers is isn't unique, so a parameter was added to specify what many answers to return.

Param(# returns several 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 Each cell, add array [1..9] for each null cell.for ($i = 0; $i-lt 9; $i + +{for ($j = 0; $j-lt 9; $j + +){if (! $SudokuMatrix [$i] [$j]) {$SudokuMatrix [$i] [$j] = 1..9}else{$SudokuMatrix [$i] [$j] = @ ($SudokuMatrix [$i] [$j]}}}# Loop each cell, calculate possible numbers to remove impossible numbers for the cell.function Goloop ($arr{$NEWARR = @ ($null) * 9 for ($i = 0; $i-lt 9; $i + +){$NEWARR [$i] = $arr [$i]. Psobject.copy ()} for ($i = 0; $i-lt 9; $i + +{for ($j = 0; $j-lt 9; $j + +)) {if ($NEWARR [$i] [$j]. Count-ne 1{for ($k = 0; $k-lt 9; $k + +)) {if ($NEWARR [$i] [$k]. Count-eq 1-and $NEWARR [$i] [$j]. Count-ne 1{$NEWARR [$i] [$j] = @ ($NEWARR [$i] [$j] |? { $_-ne $NEWARR [$i] [$k][0]})} if ($NewArr [$k] [$j]. Count-eq 1-and $NEWARR [$i] [$j]. Count-ne 1{$NEWARR [$i] [$j] = @ ($NEWARR [$i] [$j] |? { $_-ne $NEWARR [$k] [$j][0]})}}}} return $NEWARR}# Loop Each cell, if the possible number of the cell is null, means current calculation is wrong.function Verifyzero ($arr{for ($i = 0; $i-lt 9; $i + +){for ($j = 0; $j-lt 9; $j + +)) {if ($arr [$i] [$j]. Count-eq 0) {return $i, $j, $true}}} return $i, $j, $false}# Find The most less than possible numbers for a cell, return the position.function findsmallest ($arr) {foreach ($k in 2..9{for ($i = 0; $i-lt 9; $i + +){for ($j = 0; $j-lt 9; $j + +)) {if ($arr [$i] [$j]. Count-eq $k) {return $i, $j, $k}}}}}# Calculate How many cells has been confirmed, if it ' s Bayi, correct answer hit.function Countconfirmednumber ($arr{$NumberConfirmed = 0 for ($i = 0; $i-lt 9; $i + +){for ($j = 0; $j-lt 9; $j + +)) {if ($arr [$i] [$j]. Count-eq 1) {$NumberConfirmed + +}}} return $NumberConfirmed} $AnswerCount = 0$results = @() function Gocalculate ($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))-eq 81) {$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 in $OptionsStack) {# write-host "Set [$ ($Smallest [0])][$ ($Smalles T[1]] To: $Option "$NewArray [$Smallest [0]][$Smallest [1]] = @ ($Option) if ($AnswerCount-lt $ Howmanyanswersyouwanttoget) {gocalculate ($NewArray)}}}# triggergocalculate ($SudokuMatrix) # Output Answers$results | %{if ($_-eq $null) {return} write-host "Answer:"-foregroundcolor Yellow for ($i = 0; $i-lt 9; $i + +) {for ($j = 0; $j-lt 9; $j + +) {write-host "$ ($_[$i] [$j][0])"-nonewline-foregroundcolor Yellow} Wri Te-host "' N" }}              

[Powershell] an algorithm to 9x9 Sudoku.

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.