16-recursive_backtracking-2

# 16-recursive_backtracking-2 - CSE 143 Lecture 16 More...

This preview shows pages 1–4. Sign up to view the full content.

1 CSE 143 Lecture 16 More Recursive Backtracking slides created by Marty Stepp and Ethan Apter http://www.cs.washington.edu/143/ 2 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"? – How do we "make" or "un-make" a choice? – How do we know when to stop? Q Q Q Q Q Q Q Q

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 3 Naive algorithm • for (each square on board): – Place a queen there. – Try to place the rest of the queens. – Un-place the queen. – How large is the solution space for this algorithm? • 64 * 63 * 62 * . .. 1 2 3 4 5 6 7 8 1 Q ... ... ... ... ... ... ... 2 ... ... ... ... ... ... ... ... 3 ... 4 5 6 7 8 4 Better algorithm idea • Observation: In a working solution, exactly 1 queen must appear in each row and in each column. – Redefine a "choice" to be valid placement of a queen in a particular column. – How large is the solution space now? • 8 * 8 * 8 * . .. 1 2 3 4 5 6 7 8 1 Q ... ... 2 ... ... 3 Q ... 4 ... 5 Q 6 7 8
3 5 Exercise • Suppose we have a Board class with the following methods: • Write a method solveQueens that accepts a Board as a parameter and tries to place 8 queens on it safely. – Your method should stop exploring if it finds a solution. Method/Constructor Description public Board (int size) construct empty board public boolean isSafe (int row, int column) true if queen can be safely placed here public void place (int row, int column)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 09/29/2010 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

### Page1 / 8

16-recursive_backtracking-2 - CSE 143 Lecture 16 More...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online