Huddled Masses
The internet home of Joel "Jaykul" Bennett...
Browse: Home / Eight Queens in PowerShell

Eight Queens in PowerShell

By Joel 'Jaykul' Bennett on 18-Feb-2008

Well … my brother called me with an odd compiler problem with his C++ homework today (he’s working on another degree, and taking some programming courses) ... but the homework problem he was working on was the infamous N Queens problem:

Given a chess board of N x N squares, place N queens on the board in such a way than none threatens the others. In chess, a queen can move diagonally or horizontally, so solving this requires placing each queen on her own row and column, and ordered such that none of them are on the same diagonal in either direction.

The problem is solvable using a backtracking recursive algorithm: the program must place a queen on the first row and then find a spot that works for the next row, and continue for each row. If it can’t find a spot for a row, it backtracks to the previous row and uses the next legal spot on that row, and if there is none, it backtracks to the previous row, and so on.

Anyway. There’s lots of information about this stuff out there on the ‘net, including dozens of slides shows and lecture notes from dozens of schools, so I’m just going to move on to the fun part: my solution in PowerShell. I wrote it just to make sure I understood the algorithm correctly, without giving away the C++ answer as much as a quick web search would. In fact, I followed the restrictions my brother’s professor had placed on them to break it up into a specific set of functions…


## N-Queens problem using backtracking
################################################################################
## Backtracking is a problem-solving strategy that, when it reaches an impasse,
## retraces its steps in reverse order before trying a new sequence of steps.

param($size=8)       # By default, we use a board of size 8

function Check-Queen($r1,$c1,$r2,$c2) {
    return -not (
      # they can't be on the same column
      ($c1 -eq $c2) -or
      # nor on the same row
      ($r1 -eq $r2) -or
      # nor on the same diagonal
      ([Math]::Abs($r1-$r2) -eq [Math]::Abs($c1-$c2)));
}

function Out-Board($board){
    foreach($c in $board) {
        switch(0..($board.Count-1)) {
            {$_-eq$c} { write-host "Q" -fore red -nonew }
            default   { write-host "#" -nonew}
        }
        Write-Host
    }
}

function Set-Queen($board,$row,$column) {
    # Set the queen location for this row
    $board[$row]=$column;
   # if this is the last row, then we're done
    if($row+1 -eq $board.Count) { return $true }
   
   # place the queen in the next row
   # try every column ....
    for($c=0;$c -lt $board.Count; $c++) {
        $clean = $true
      # check it against every prior row
        for($r=0; $r -le $row; $r++) {
            $clean = $clean -and (Check-Queen $r $board[$r] ($row+1) $c)
            if(!$clean) { break; }
        }
      # if we found the spot, recurse the next row...
        if($clean -and( Set-Queen $board ($row+1) $c )) {
            return $true;
        }
    }
    return $false
}

$board = new-object int[] $size

# if starting in 0,0 doesn't work 0,1 will
if(Set-Queen $board 0 0){
   Out-Board $board
}elseif(Set-Queen $board 0 1){
   Out-Board $board
}

Similar Posts:

  • What Scope Am I In?
  • Better error messages for PowerShell ValidatePattern
  • Disabling Events in ShowUI
  • Arrange – Act – Assert: Intuitive Testing
  • More Custom Attributes for PowerShell (Parameter Transformation)

Posted in Huddled | Tagged Algorithms, Backtracking, Challenges, PowerShell, Programming, Recursion, Scripting

« Previous Next »

Lijit Search

Tags

.Net .Net 2008 Scripting Games Automation Bugs Design Development Funny Gadgets GeoShell GUI Huddled Masses Internet licensing Microsoft Modules My Software News Personal PInvoke Pipeline Politics PoshCode PoshConsole PowerBoots PowerShell PowerShell Functions PowerTips Rants Recommender Repository Scripting ShowUI Software Solutions Textile Tips User Group UserInterface WalkThrough WebHosting Windows 7 WordPress WPF Xml

About Huddled Masses

This is web site is dedicated to the musings of Joel Bennett (aka Jaykul) about technology, software, software development, the web, and the world.

Any resemblance of the views expressed and the views of my employer, my terminal, or the view out my window are purely coincidental. The resemblance between them and my own views is non-deterministic. The question of the existence of views in the absence of anyone to hold them is left as an exercise for the reader.

P.S.: I occasionally link to things I think are great. When I do, I occasionally find a "referral code" so I can make a little cash. I promise that I don't link to anything just because of that cash (I wouldn't cross the street for the amount of cash those links bring in, never mind write a whole blog post) ... but I do not promise that things I link to will stay great as time passes, nor that you will agree with me about their greatness!

Archives

  • April 2012
  • February 2012
  • January 2012
  • October 2011
  • August 2011
  • July 2011
  • June 2011
  • March 2011
  • February 2011
  • January 2011

Copyright © 2012 Joel Bennett.

Powered by WordPress and Hybrid.