handout

advertisement
/**
*
*
*
*
*
Class Maze1 illustrates navigating a maze using a 2-d array,
where the solution can be found simply by always going down
if possible, otherwise going to the right.
Running the program gives:
[1,1], [1,2], [2,2], [3,2], [4,2], [4,3], [4,4], [4,5], [5,5], [6,5], [7,5],
[8,5], [8,6], [8,7], [8,8],
Got to destination!
[4,6], [1,3],
*
* Problems:
*
What if the solution requires going up first?
*
More moves are displayed after the solution is found.
*/
public class Maze1
{
private static int
private static int
private int endRow
private int endCol
startRow
startCol
= 8;
= 8;
= 1;
//
= 1;
//
// ending
// ending
starting row, static so main() can use it
starting column static so main() can use it
row
column
// declare the actual maze as a 2-dimensional array
// A 1 is a wall, while a 0 is an open pathway.
private int[][] maze = {
/*
0 1 2 3 4 5 6 7 8 9 */
/* 0 */ {1,1,1,1,1,1,1,1,1,1},
/* 1 */ {1,0,0,0,1,1,1,1,1,1},
/* 2 */ {1,1,0,1,1,1,0,0,1,1},
/* 3 */ {1,1,0,1,1,1,0,1,1,1},
/* 4 */ {1,1,0,0,0,0,0,1,1,1},
/* 5 */ {1,1,1,1,1,0,1,1,1,1},
/* 6 */ {1,0,1,1,1,0,1,0,0,1},
/* 7 */ {1,1,1,1,1,0,1,0,1,1},
/* 8 */ {1,1,0,0,0,0,0,0,0,1},
/* 9 */ {1,1,1,1,1,1,1,1,1,1}
};
/**
* Main method to allow execution from the command line
*/
public static void main(String[] args)
{
Maze1 instanceOfMaze1 = new Maze1();
// default constructor
instanceOfMaze1.makeMove( startRow, startCol);
}
public void makeMove( int row, int col)
{
// display location
System.out.print("[" + row + "," + col + "], ");
// see if we've reached the destination
if (( row == endRow) && ( col == endCol)) {
System.out.println("\n Got to destination! ");
}
// see if we can move down
if ( maze[ row+1][ col] != 1) {
makeMove( row+1, col);
// recursive call, moving down
}
if (maze[ row][ col+1] != 1) {
// see if we can move right
makeMove( row, col+1);
// recursive call, moving right
}
// Note that we never move up or left!
}//end makeMove
}
/* Class Maze2 illustrates navigating a maze using a 2-d array.
* The maze is changed so that solution requires moving in all directions.
* On each move we first try to move down, then right, then left, then up.
* Running the program gives:
[1,1], [1,2], [2,2], [3,2], [4,2], [5,2], [4,2], [5,2], [4,2], [5,2], [4,2]...
....(keeps repeating [4,2], [5,2]).... *
* Problems:
*
Once we get to [4,2] we dead-end, and so try to move up. This takes us
*
back to where we came from, and we bounce back and forth forever.
*/
public class Maze2
{
private static int
private static int
private int endRow
private int endCol
startRow
startCol
= 8;
= 8;
= 1;
//
= 1;
//
// ending
// ending
starting row, static so main() can use it
starting column static so main() can use it
row
column
// declare the actual maze as a 2-dimensional array. 1 is a wall, 0 is a pathway.
private int[][] maze = {
/*
0 1 2 3 4 5 6 7 8 9 */
/* 0 */ {1,1,1,1,1,1,1,1,1,1},
/* 1 */ {1,0,0,0,1,1,1,1,1,1},
/* 2 */ {1,1,0,1,1,1,0,0,1,1},
/* 3 */ {1,1,0,0,0,0,0,1,1,1},
/* 4 */ {1,1,0,1,1,0,1,1,1,1},
/* 5 */ {1,1,0,1,0,0,1,1,1,1},
/* 6 */ {1,1,1,1,0,1,0,0,0,1},
/* 7 */ {1,1,1,1,0,0,0,1,0,1},
/* 8 */ {1,1,0,0,0,1,1,1,0,1},
/* 9 */ {1,1,1,1,1,1,1,1,1,1}
};
public static void main(String[] args)
{
Maze2 instanceOfMaze2 = new Maze2();
// default constructor
instanceOfMaze2.makeMove( startRow, startCol);
}
public void makeMove( int row, int col)
{
System.out.print("[" + row + "," + col + "], ");
// see if we've reached the destination
if (( row == endRow) && ( col == endCol)) {
System.out.println("\n Got to destination! ");
}
// display location
// Attempt moves in this order: down, right, left, up
if ( maze[ row+1][ col] != 1) {
// see if we can move down
makeMove( row+1, col);
// recursive call, moving down
}
if (maze[ row][ col+1] != 1) {
// see if we can move right
makeMove( row, col+1);
// recursive call, moving right
}
if (maze[ row][ col-1] != 1) {
// see if we can move left
makeMove( row, col-1);
// recursive call, moving left
}
if (maze[ row-1][ col] != 1) {
// see if we can move up
makeMove( row-1, col);
// recursive call, moving up
}
}//end makeMove
}
/**
*
*
*
*
*
*
*
*
*
*
*
*
*/
Class Maze3 illustrates navigating a maze.
An important change here is that the 2-dimensional array is represented
in one dimension, which makes it easier to write some parts of the code. To
move to a different square we now only need to add a single number to the
current square. The order of attempted moves is changed to be left, up,
right, down.
This version still does not solve the problems of stopping once the
solution is found, or preventing an infinite loop of moving between two
squares.
Running the program gives:
11, 12, 11, 12, 11, 12,...(keeps repeating until you get StackOverflowError )
Problems:
Bounces between two moves, doesn't stop at end, doesn't display solution
public class Maze3
{
private static int start = 11; // this is static so main() can use it
private static int end = 88;
// this is static so main() can use it
private int[] maze = {
/*
0 1 2 3 4 5 6 7 8 9 */
/* 0 */ 1,1,1,1,1,1,1,1,1,1,
/*10 */ 1,0,0,0,1,1,1,1,1,1,
/*20 */ 1,1,0,1,1,1,0,0,1,1,
/*30 */ 1,1,0,1,1,1,0,1,1,1,
/*40 */ 1,1,0,0,0,0,0,1,1,1,
/*50 */ 1,1,0,1,1,0,1,1,1,1,
/*60 */ 1,0,0,1,0,0,1,1,1,1,
/*70 */ 1,1,1,1,0,1,0,0,0,1,
/*80 */ 1,1,0,0,0,0,0,1,0,1,
/*90 */ 1,1,1,1,1,1,1,1,1,1};
private int[] moves = {-1,-10,1,10};
// left, up, right, down
/**
* Main method to allow execution from the command line
*/
public static void main(String[] args)
{
Maze3 instanceOfMaze3 = new Maze3();
// default constructor used
instanceOfMaze3.makeMove( start);
instanceOfMaze3.displaySolution( end);
}
/**
* Try all possible next moves from the perspective of the current square.
*/
public void makeMove( int current)
{
System.out.print(current + ", ");
if ( current == end) {
System.out.println("Got to destination!");
return;
// stop trying moves at this level
}
for (int i=0; i<4; i++) {
int next = current + moves[i];
if (maze[next] != 1) {
}
makeMove( next);
}
}//end makeMove
public void displaySolution( int current)
{
}
}
// calculate next move
// recursively make next move
Download