{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS112_11_Slides-RecursiveBacktracking

# CS112_11_Slides-RecursiveBacktracking - Recursive...

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

1 CS112 Fundamentals of Programming Abstractions Recursive Backtracking Yinglin Wang CS Dept., SJTU 2 CS112 Fundamentals of Programming Abstractions 1. Review of the former lecture 2. Recursive Backtracking Contents of this lecture:

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

View Full Document
3 CS112 Fundamentals of Programming Abstractions void MoveTower(int n, char start, char finish, char temp) { if (n = = 1) { Move a single disk from start to finish. else { Move a tower of size n-1 from start to temp. Move a single disk from start to finish. Move a tower of size n-1 from temp to finish. } } Pseudo-code: Once you’ ve identified the simple case and recursive decomposition, all you need to do is to follow the standard recursive paradigm! 1. Review: Tower of Hanoi 4 CS112 Fundamentals of Programming Abstractions Validating the strategy in tower of Hanoi void MoveTower(int n, char start, char finish, char temp) { if (n = = 1) { Move a single disk from start to finish. else { Move a tower of size n-1 from start to temp. Move a single disk from start to finish. Move a tower of size n-1 from temp to finish. } } -Be careful: You must make sure that the new problems are identical in form of the original! But there are differences in forms: 1.The original problem: Move a tower of size n from start to finish ( there are no any disks on the pole of temp ). 2.The decomposed problem: when you move a tower of size n-1 from start to temp, you’ ve left a disk behind on the stating pole. Notice: this difference doesn’ t change the nature because the disk left behind must be larger than any disks in the current sub- tower, so we need not care how many disks left behind. It will not affect the solution.
5 CS112 Fundamentals of Programming Abstractions Review: Fractals “Nature uses as little as possible” 6 CS112 Fundamentals of Programming Abstractions M.C. Escher: smaller and smaller Fractals

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

View Full Document
7 CS112 Fundamentals of Programming Abstractions 2. Recursive backtracking Today we will apply the notion of recursive backtracking to the problem of finding our way through a maze. Mazes have been around for a long time. This pattern (actually a labyrinth) is from Chartres Cathedral, but it existed in Crete a thousand years earlier, and it has been found in other cultures as well. 8 CS112 Fundamentals of Programming Abstractions Introduction to 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 (or at least the part of it the problem is concerned with), and what we are trying to do is find a path from the starting node to a node that represents success.
9 CS112 Fundamentals of Programming Abstractions Walking through mazes can be modeled as a series of state nodes. Each node represents a state which specifies where you are located. At each node a decision is made which leads to other nodes.

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.

{[ snackBarMessage ]}

### Page1 / 20

CS112_11_Slides-RecursiveBacktracking - Recursive...

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

View Full Document
Ask a homework question - tutors are online