[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Back to Stack lab. On to Dictionaries.
Held Tuesday, May 4
Summary
Contents
Notes
+---+---+---+ | 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 |
+---+---+---+ +---+---+---+
History
Back to Stack lab. On to Dictionaries.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [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/99S/Outlines/outline.40.html
Source text last modified Fri Apr 16 10:54:07 1999.
This page generated on Fri Apr 16 11:00:18 1999 by SiteWeaver. Validate this page's HTML.
Contact our webmaster at rebelsky@math.grin.edu