Download presentation
Presentation is loading. Please wait.
Published byLoreen Phillips Modified over 9 years ago
1
NxN Queens Problem
2
-- -- Q- -- -Q -- -- Q- -- -Q Q- -- QQ -- Q- Q- Q- -Q QQ -- -Q -- -Q Q- -Q -Q Q- Q- -Q Q- -- Q- -- QQ Q- -Q -Q -Q -- QQ -- -Q START Backtracking Attempt to Solve 2 Queens on 2x2 Board FAIL
3
-- -- R- -- -R -- -- R- -- -R R- -- RR -- R- R- R- -R RR -- -R -- -R R- -R -R R- R- -R R- -- R- -- RR R- -R -R -R -- RR -- -R START Backtracking Attempt to Solve 2 ROOKS on 2x2 Board FAIL GOAL FAIL GOAL
4
ALGORITHM (First Attempt) GOAL: Given empty NxN board, Return board with N Queens in SAFE positions OR Return empty board if impossible ALGORITHM: Test Queen @ position in board If Queen is safe then add Queen to board at position try NEXT Queen at NEXT position in board (recursive) Else try CURRENT Queen at NEXT position in board (recursive) STOP when position too large for board (failure) STOP when out of Queens (success)
5
; Precondition : BOARD is an NXN board ; POSITION is the valid location on the board ; Postcondition: Always returns '(). However this function uses ; the non-standard DISPLAY side-effect to display ; the contents of a board to standard output. (define (displayBoard board position) (cond ; BASE CASE, position is too big, we're done [(equal? (getBoardSize board) position) '()] ; RECURSIVE CASE, we're end of a ROW [(equal? (modulo (+ position 1) (getBoardDimension board)) 0) (let ( [a (display (getBoardValue board position))] [b (display "\n")] ) (displayBoard board (+ position 1)))] ; RECURSIVE CASE, we're anywhere else in ROW [else (let ( [a (display (getBoardValue board position))] [b (display " ")] ) (displayBoard board (+ position 1)))] ) Function to Display Chess Board Note “non-standard” features: - LET (sequence) - DISPLAY (side-effects)
6
ALGORITHM (Second Attempt) (define (computeQueens board position queensLeft) (cond ; BASE CASE, no queens left, we're done and we have a SOLUTION [(= queensLeft 0) board] ; BASE CASE, position too big, we're done and we've FAILED [(> position (- (getBoardSize board) 1)) '()] ; RECURSIVE CASE, placing queen at current position is SAFE AND recursive ; calls with the current board will lead to a solution, SO return the ; solution derived from recursive calls with the current board. [(and (isSafeBoard board 0 position) (not (empty? (computeQueens (setBoardValue board position 'Q) (+ position 1) (- queensLeft 1))))) (computeQueens (setBoardValue board position 'Q) (+ position 1) (- queensLeft 1))] ; RECURSIVE BACKTRAKING CASE, placing queen at current position won't work ; either because the queen is in immediate danger, OR we can't place enough ; queens on the board after placing this queen at the current position. Try ; placing the current queen at the next position on the board. [else (computeQueens board (+ position 1) queensLeft)] ) ; Call to solve NQueens problem on a 4X4 board (displayBoard (computeQueens (generateBoard 6) 0 6) 0)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.