[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Project] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Back to Heaps and Heap Sort. On to Stack Lab.
Held Friday, November 5, 1999
Overview
Today, we'll visit an application of stacks and queues: automated puzzle solving. Along the way, we'll consider some object-oriented design issues.
Notes
Contents
Summary
+---+---+---+ | 1 | 2 | 3 | +---+---+---+ | 4 | 5 | 6 | +---+---+---+ | 7 | 8 | | +---+---+---+
+---+---+---+ | 4 | | 2 | +---+---+---+ | 5 | 1 | 8 | +---+---+---+ | 7 | 6 | 3 | +---+---+---+
+---+---+---+ | | 3 | 5 | +---+---+---+ | 2 | 4 | 6 | +---+---+---+ | 7 | 1 | 8 | +---+---+---+
+---+---+---+---+ | S | | + +---+ +---+ | | | | +---+ + +---+ | | | + + +---+---+ | | | | +---+ + + + | | | F | +---+---+---+---+
Puzzle object (or interface) would
support:
Move class (or
interface).
while (!solved) {
pick a legal move
make that move
}
// As long as we haven't found a solution AND // there is a board state + legal move combination we haven't tried // Pick one of those combinations // Make the move
/**
* Find a solution to a puzzle using a brute-force method.
* Pre: The puzzle has been initialized.
* Post: The solution is printed.
*/
public void solve(Puzzle p) {
Set checked = new Set(); // The boards we've already checked
MoveList movesToMake; // The moves from current pos.
Linear todo = new Linear(); // The boards we still need to check
Puzzle current; // The current state of the puzzle
Puzzle next; // The next state of the puzzle
// Start at the beginning
current = p;
current.reset();
checked.add(current);
todo.add(current);
// Keep going until we find a solution (exit within loop) or
// run out of things to try.
while (!todo.empty()) {
// Get the next state of the puzzle to try.
current = todo.remove();
// Is it a solution?
if (current.solved()) {
print "Solved the puzzle. Guess how."
return;
}
// If not, consider all alternatives
movesToMake = current.legalMoves();
for each move in movesToMake { // This is not legal Java
// Make the move
next = current.clone();
next.makeMove(move);
// If we haven't seen the position, add it as something to consider
if (!checked.contains(next)) {
checked.add(next);
todo.add(next);
} # if we haven't seen this position yet
} // for each
} // while
print "The puzzle had no solution.";
} // solve
Starting-State
/ | \
move move move
/ | \
State01 State02 State03
/ |
move move ........
/ |
State04 State05
+---+---+---+
| 4 | | 2 |
+---+---+---+
| 5 | 1 | 8 |
+---+---+---+
| 7 | 6 | 3 |
+---+---+---+
|
+------------------+------------------+
| | |
move the 4 move the 1 move the 2
+---+---+---+ +---+---+---+ +---+---+---+
| | 4 | 2 | | 4 | 1 | 2 | | 4 | 2 | |
+---+---+---+ +---+---+---+ +---+---+---+
| 5 | 1 | 8 | | 5 | | 8 | | 5 | 1 | 8 |
+---+---+---+ +---+---+---+ +---+---+---+
| 7 | 6 | 3 | | 7 | 6 | 3 | | 7 | 6 | 3 |
+---+---+---+ +---+---+---+ +---+---+---+
| ... ...
|
+------------------+
| |
move the 4 move the 2
+---+---+---+ +---+---+---+
| 4 | | 2 | | 4 | 2 | |
+---+---+---+ +---+---+---+
| 5 | 1 | 8 | | 5 | 1 | 8 |
+---+---+---+ +---+---+---+
| 7 | 6 | 3 | | 7 | 6 | 3 |
+---+---+---+ +---+---+---+
Tuesday, 10 August 1999
Thursday, 4 November 1999
Back to Heaps and Heap Sort. On to Stack Lab.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Project] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99F/Outlines/outline.38.html
Source text last modified Thu Nov 4 21:11:33 1999.
This page generated on Thu Nov 4 21:14:41 1999 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu