CS112_12_slides-gametrees

CS112_12_slides-gametrees - Recursive Backtrackingcont....

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS112 Fundamentals of Programming Abstractions Recursive Backtracking- cont. Yinglin Wang CS Dept., SJTU 2 CS112 Fundamentals of Programming Abstractions Backtracking § Many problems can be thought of as a search through a space consisting of various nodes. § Each node represents a state of the world. § what we are trying to do is find a path from the starting node to a node that represents success.
Background image of page 1

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

View Full DocumentRight Arrow Icon
3 CS112 Fundamentals of Programming Abstractions backtracking Start Success! Success! Failure Key Points: If a node only leads to failure go back to its "parent“node. Try other alternatives. If these all lead to failure then more backtracking may be necessary. 4 CS112 Fundamentals of Programming Abstractions backtracking Backtracking: When you make a sequence of decision choices as you search a goal through a state space., if you reach a dead-end or other-wise discover that you have made a incorrect choice somewhere along the way, you have to return to a previous decision point and try a different path. This kind of methods is called backtracking. The basic strategy is to write programs that can backtracking to previous decision points if those choices lead to dead-end; You can use recursion to implement backtracking search .
Background image of page 2
5 CS112 Fundamentals of Programming Abstractions The Pseudocode of Recursive Backtracking If you are already at a solution, report success. for (every possible choice in the current position ) { Make that choice and take one step along the path. Use recursion to solve the problem from the new position. If the recursive call succeeds, report the success to the next higher level. Back out of the current choice to restore the state at the beginning of the loop. } Report failure. 6 CS112 Fundamentals of Programming Abstractions backtracking Backtracking is strategy of “generate and test”
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions Backtracking Algorithm for maze If the current square is outside, return TRUE to indicate that a solution has been found. If the current square is marked, return FALSE to indicate that this path has been tried. Mark the current square. for ( each of the four compass directions ) { if ( this direction is not blocked by a wall ) { Move one step in the indicated direction from the current square. Try to solve the maze from there by making a recursive call. If this call shows the maze to be solvable, return TRUE to indicate that fact. } } Unmark the current square. Return FALSE to indicate that none of the four directions led to a solution. 8 CS112 Fundamentals of Programming Abstractions n -Queens Problem(exercise 8, chp6 ) Given: n-queens and an nxn chess board Find: A way to place all n queens on the board such that no queens are attacking another queen. i.e, every two queens are not allowed
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/06/2010 for the course CS 112 taught by Professor Yinlinwang during the Spring '08 term at Shanghai Jiao Tong University.

Page1 / 35

CS112_12_slides-gametrees - Recursive Backtrackingcont....

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online