NxN Queens Problem. -- -- 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.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006.
PROLOG 8 QUEENS PROBLEM.
Computers playing games. One-player games Puzzle: Place 8 queens on a chess board so that no two queens attack each other (i.e. on the same row, same.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
CPS 100, Fall Backtracking by image search.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
B ACKTRACK SEARCH ALGORITHM. B ACKTRACKING Suppose you have to make a series of decisions, among various choices, where You don’t have enough information.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
SEARCH APPLICATIONS David Kauchak CS30 – Spring 2015.
Introduction This document describes the design of a single player interactive triangular peg board game with a variable number of pegs.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Lecture 4 1) RECURSION 2)BACKTRACKING 3)LOOK AHEAD.
Chapter 2: Design of Algorithms
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Backtracking.
CSE (c) S. Tanimoto, 2007 Search 1: State Spaces 1 State-Space Search Outline: Demonstration with T* State spaces, operators, moves A Puzzle: The.
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
Search Much of AI has traditionally revolved around search –Search means “searching among all the possibilities for an answer” (or possibly the best answer)
Section 1.1: Integer Operations and the Division Algorithm
Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that.
1 CS 132 Spring 2008 Chapter 6 Recursion Read p Skip example 6-3 (Fibonacci), 6-4 (Hanoi) Read example 6-5 (p. 387)
COSC2007 Data Structures II
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
BackTracking CS335. N-Queens The object is to place queens on a chess board in such as way as no queen can capture another one in a single move –Recall.
Two Dimensional Arrays
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Recursion When to use it and when not to use it. Basics of Recursion Recursion uses a method Recursion uses a method Within that method a call is made.
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Please snarf the code for today’s class. Then think about the famous 8-Queen’s Puzzle. The question is: is there a way to arrange 8 queens on a (8x8) chessboard.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze  Certain mazes can be solved using the “right-hand”
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Backtracking & Brute Force Optimization Intro2CS – weeks
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
N- Queens Solution with Genetic Algorithm By Mohammad A. Ismael.
 A family of optimization methods that search for an optimum minimum or maximum for a given problem (but never finds it ).  The methods are best suited.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
1 CSC 384 Lecture Slides (c) , C. Boutilier and P. Poupart CSC384: Lecture 16  Last time Searching a Graphplan for a plan, and relaxed plan heuristics.
Solving multi step equations. 12X + 3 = 4X X 12X + 3 = 3X X 9X + 3 = X = X =
Solving equations with variable on both sides Part 1.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Unit – 5: Backtracking For detail discussion, students are advised to refer the class discussion.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
CSG3F3/ Desain dan Analisis Algoritma
BackTracking CS255.
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
Sit-In Lab 1 Ob-CHESS-ion
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
1) RECURSION 2) BACKTRACKING 3) LOOK AHEAD
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
Exercise: Dice roll sum Write a method diceSum similar to diceRoll, but it also accepts a desired sum and prints only arrangements that add up to.
Exercise: Dice roll sum
Exercise: Dice roll sum
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Presentation transcript:

NxN Queens Problem

-- -- 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

-- -- 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

ALGORITHM (First Attempt) GOAL: Given empty NxN board, Return board with N Queens in SAFE positions OR Return empty board if impossible ALGORITHM: Test 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)

; 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)

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)