1 / 6

NxN Queens Problem

NxN Queens Problem. START. -. -. -. -. Q. -. -. Q. -. -. -. -. -. -. -. -. Q. -. -. Q. Q. -. Q. Q. Q. -. Q. -. Q. Q. -. Q. -. Q. -. Q. Q. -. -. Q. -. -. -. -. -. -. -. -. Q. -. -. Q. -. -. -. -. Q. -. -. Q. Q. -. Q. -. Q. -. Q. Q.

Download Presentation

NxN Queens Problem

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. NxN Queens Problem

  2. START - - - - Q - - Q - - - - - - - - Q - - Q Q - Q Q Q - Q - Q Q - Q - Q - Q Q - - Q - - - - - - - - Q - - Q - - - - Q - - Q Q - Q - Q - Q Q FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL FAIL Q - - Q - - - - - Q - Q Q Q - Q FAIL FAIL FAIL FAIL Backtracking Attempt to Solve 2 Queens on 2x2 Board

  3. START - - - - R - - R - - - - - - - - R - - R R - R R R - R - R R - R - R - R R - - R - - - - - - - - R - - R - - - - R - - R R - R - R - R R FAIL FAIL FAIL FAIL FAIL GOAL GOAL FAIL FAIL GOAL FAIL FAIL R - - R - - - - - R - R R R - R GOAL FAIL FAIL FAIL Backtracking Attempt to Solve 2 ROOKS on 2x2 Board

  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. Function to Display Chess Board ; 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)))] ) ) 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)

More Related