22-Recursive-Backtracking

22-Recursive-Backtracking - CS106X Winter 2008 H a 22...

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

View Full Document Right Arrow Icon
CS106X Han 22 Winter 2008 February 1, 2008 Recursive Backtracking Recursive Backtracking So far, all of the recursive algorithms we have seen have shared one very important property: each time a problem was recursively decomposed into a simpler instance of the same problem, only one such decomposition was possible; consequently, the algorithm was guaranteed to produce a solution to the original problem. Today we will begin to examine problems with several possible decompositions from one instance of the problem to another. That is, each time we make a recursive call, we will have to make a choice as to which decomposition to use. If we choose the wrong one, we will eventually run into a dead end and find ourselves in a state from which we are unable to solve the problem immediately and unable to decompose the problem any further; when this happens, we will have to backtrack to a "choice point" and try another alternative. You’ll want to read Chapter 6 of the reader very carefully. We’ll complement the reader with a good number of additional examples not in the reader. If we ever solve the problem, great, we’re done. Otherwise, we need to keep exploring all possible paths by making choices and, when they prove to have been wrong, backtracking to the most recent choice point. What’s really interesting about backtracking is that we only back up in the recursion as far as we need to go to reach a previously unexplored choice point. Eventually, more and more of these choice points will have been explored, and we will backtrack further and further. If we happen to backtrack to our initial position and find ourselves with no more choices from that initial position, the particular problem at hand is unsolvable. Morphing Write a function Morph which recursively attempts to morph the start word into the destination word by a series of transformations. A transformation is changing one letter from the start word into the letter at the corresponding position in the destination word . Each word formed along the way must be a valid word (as reported by the dictionary from the Boggle assignment that went out on Wednesday). Assume the lexicon has been opened and is ready for use. Remember the member function to check if a word is in the lexicon is: bool Lexicon::containsWord(string word); act end act ant and end read boot read bead beat boat boot The two parameters to Morph are the start word and the target word. Your function should return true if and only if a morph could be found (i.e. you do not have to keep track of the steps, just report whether you found a morph or not).
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 bool Morph(string start, string dest, Lexicon& lex) { if (start == dest) return true; if (!lex.containsWord(start)) return false; for (int i = 0; i < start.length(); i++) { if (dest[i] != start[i]) { char saved = start[i]; start[i] = dest[i]; if (Morph(lex, start, dest)) return true; start[i] = saved; } } return false; } Playing Dominoes The game of dominoes is played with rectangular pieces composed of two connected squares, each of which is marked with a certain number of dots.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/18/2008 for the course CS 106X taught by Professor Cain,g during the Winter '08 term at Stanford.

Page1 / 11

22-Recursive-Backtracking - CS106X Winter 2008 H a 22...

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

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