首页 > 系统服务 > 详细

[Powershell] An algorithm to 9x9 sudoku.

时间:2015-03-02 10:56:49      阅读:272      评论:0      收藏:0      [点我收藏+]

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

技术分享

param(
    # 返回几个答案
    [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 of 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 have been confirmed, if it‘s 81, 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 "81 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])][$($Smallest[1])] to: $Option"
        $NewArray[$Smallest[0]][$Smallest[1]] = @($Option)
        if($AnswerCount -lt $HowManyAnswersYouWanttoGet){
            GoCalculate($NewArray)
        }
    }
}
# Trigger
GoCalculate($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
        }
        Write-Host "`n"
    }
}

 

[Powershell] An algorithm to 9x9 sudoku.

原文:http://www.cnblogs.com/LarryAtCNBlog/p/4307965.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!