Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
0 of .
Results for:
P. 1
h22 section handout 3

# h22 section handout 3

Ratings: (0)|Views: 15|Likes:

### Availability:

See more
See less

02/01/2013

pdf

text

original

CS106B
Handout #22
Yves Lu
July 15, 2008
Section Handout #3
Problem 1: List Mnemonics
On the standard Touch-Tone\u2122 telephone dial, the digits are mapped onto the alphabet
(minus the lettersQ andZ) as shown in the diagram below:

In order to make their phone numbers more memorable, service providers like to find
numbers that spell out some word (called amn emon ic) appropriate to their business that
makes that phone number easier to remember. For example, the phone number for a
recorded time-of-day message in some localities is 637-8687 (NERVOUS).

Imagine that you have just been hired by a local telephone company to write a function
ListMnemonicsthat will generate all possible letter combinations that correspond to a
given number, represented as a string of digits. For example, if you call
ListMnemonics("723")your program should generate the following 27 possible letter
combinations that correspond to that prefix:

PAD PBD PCD RAD RBD RCD SAD SBD SCD PAE PBE PCE RAE RBE RCE SAE SBE SCE PAF PBF PCF RAF RBF RCF SAF SBF SCF

The function declaration is listed below:
void ListMnemonics(string str);
\u20132 \u2013
Problem 2: Shortest Path Through a Maze
In many mazes, there are multiple paths. For example, the diagrams below show three
solutions for the same maze:
length = 13
length = 15
length = 13
None of these solutions, however, is optimal. The shortest path through the maze has a
path length of 11:
As a starting point, begin by considering the general maze solution covered in the reader:

/*
* Function: SolveMaze
* Usage: if (SolveMaze(pt)) . . .

*\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013\u2013

* This function attempts to generate a solution to the current
* maze from point pt. SolveMaze returns true if the maze has
* a solution and false otherwise. The implementation uses
* recursion to solve the sub-mazes that result from marking the
* current square and moving one step along each open passage.
*/
bool SolveMaze(pointT pt)
{

if (OutsideMaze(pt))
return true;
if (IsMarked(pt))

return false;
MarkSquare(pt);
for (directionT dir = North; dir <= West; dir=directionT(dir + 1))
{

if (!WallExists(pt, dir) && SolveMaze(AdjacentPoint(pt, dir)))
{
return true;
}
}
\u20133 \u2013
UnmarkSquare(pt);
return false;
}Write a function
int ShortestPathLength(pointT pt);
that returns the length of the shortest path in the maze from the specified position to any
exit. If there is no solution to the maze,ShortestPathLength should return the constant
NoSolution, which is defined to have a value larger than the maximum permissible path
length, as follows:
static const int NoSolution = 10000;
Problem 3: Edit "Levenshtein" Distance

In 1965, Russian scientist Vladimir Levenshtein devised the notion of "Levenshtein
distance" to measure the similarity between strings. The distance between two identical
strings is 0, otherwise it is the minimum number of deletions, insertions, or substitutions
required to transform one string into the other. For example, you can transform "happy"
into "sappy" with just one substitution (change 's' to 'h') so the distance is 1. Changing
"bite" into "built" requires two insertions and one deletion, so the distance is 3. The
greater the Levenshtein distance between two strings, the more dissimilar the strings are.
The Levenshtein distance algorithm can be used for spell checking, speech recognition,
DNA analysis, and more.

Write a function to compute the Levenshtein distance between two given strings:
int EditDistance(string s, string t);

The two parameters are the source and target strings. An "edit" is substituting one
character for another, deleting a character, or inserting a character. The function returns
the minimum number of edits required to transform the source string into the target

string. You don\u2019t need to change the source or target string or allocate additional strings.
Problem 4: Knights Tour

In chess, a knight moves in an L-shaped pattern: two squares in one direction horizontally
or vertically, and then one square at right angles to that motion. For example, the white
knight in the following diagram can move to any of the eight squares marked with a black
cross: