本程序实现迷宫求解,比较简单。算法是:输入一个任意二维数组(ctrl+z代表输入完成),输入迷宫人入口(用m表示)和出口(用e表示)。结果输出路径。所用的IDE是eclipse。{code}package cn.mazeproject;public class MazeCell {
public int x, y; public MazeCell() { } public MazeCell(int i, int j) {
x = i;
y = j;
} public boolean equals(MazeCell cell) {
return x == cell.x && y == cell.y;
}
}{code}
{code}
package cn.mazeproject;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Stack;public class Maze {
private int rows = 0, cols = 0;
private char[][] store;
private MazeCell currentCell, exitCell = new MazeCell(),
entryCell = new MazeCell();
private final char exitMarker = 'e', entryMarker = 'm', visited = '#';
private final char passage = '0', wall = '1';
private Stack<MazeCell> mazeStack = new Stack<MazeCell>(); public Maze() {
int row = 0, col = 0;
Stack<String> mazeRows = new Stack<String>();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(isr);
// Scanner in = new Scanner(System.in);
System.out.println("Enter a rectangular maze using the following"
+ "character:\nm-entry\ne-exit\n1-wall\n0-passage"
+ "Enter one line at a time; end with Ctrl-z:");
// String str = in.next();
try{
String str = buffer.readLine();
while (str != null) {
row++;
cols = str.length();
str = "1" + str + "1";
mazeRows.push(str);
if (str.indexOf(exitMarker) != -1) {
exitCell.x = row;
exitCell.y = str.indexOf(exitMarker);
} if (str.indexOf(entryMarker) != -1) {
entryCell.x = row;
entryCell.y = str.indexOf(entryMarker);
}
// str = in.next();
str = buffer.readLine();
}
} catch (IOException eof) {
}
rows = row;
store = new char[row + 2][];
store[0] = new char[cols + 2];
for (; !mazeRows.isEmpty(); row--) {
store[row] = ((String) mazeRows.pop()).toCharArray(); }
store[rows + 1] = new char[cols + 2];
for (col = 0; col <= cols + 1; col++) {
store[0][col] = wall;
store[rows + 1][col] = wall;
}
} public void display(PrintStream out) {
for (int row = 0; row <= rows + 1; row++) {
out.println(store[row]);
}
out.println();
} public void pushUnvisited(int row, int col) {
if (store[row][col] == passage || store[row][col] == exitMarker)
mazeStack.push(new MazeCell(row, col));
} public void exitMaze(PrintStream out) {
currentCell = entryCell;
out.println();
while (!currentCell.equals(exitCell)) {
int row = currentCell.x;
int col = currentCell.y;
display(System.out);
if (!currentCell.equals(entryCell))
store[row][col] = visited;
pushUnvisited(row - 1, col);
pushUnvisited(row + 1, col);
pushUnvisited(row, col - 1);
pushUnvisited(row, col + 1);
if (mazeStack.isEmpty()) {
display(out);
out.println("Failure");
return;
} else
currentCell = (MazeCell) mazeStack.pop();
}
display(out);
out.println("Success");
} public static void main(String[] args) {
(new Maze()).exitMaze(System.out);
}}{code}
以下是一次运行的过程及错误:
Enter a rectangular maze using the followingcharacter:
m-entry
e-exit
1-wall
0-passageEnter one line at a time; end with Ctrl-z:
1 1 0 0
0 0 0 e
0 0 m 1
111111111
11 1 0 01
10 0 0 e1
111111111Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at cn.mazeproject.Maze.pushUnvisited(Maze.java:72)
at cn.mazeproject.Maze.exitMaze(Maze.java:85)
at cn.mazeproject.Maze.main(Maze.java:101)
public int x, y; public MazeCell() { } public MazeCell(int i, int j) {
x = i;
y = j;
} public boolean equals(MazeCell cell) {
return x == cell.x && y == cell.y;
}
}{code}
{code}
package cn.mazeproject;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Stack;public class Maze {
private int rows = 0, cols = 0;
private char[][] store;
private MazeCell currentCell, exitCell = new MazeCell(),
entryCell = new MazeCell();
private final char exitMarker = 'e', entryMarker = 'm', visited = '#';
private final char passage = '0', wall = '1';
private Stack<MazeCell> mazeStack = new Stack<MazeCell>(); public Maze() {
int row = 0, col = 0;
Stack<String> mazeRows = new Stack<String>();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(isr);
// Scanner in = new Scanner(System.in);
System.out.println("Enter a rectangular maze using the following"
+ "character:\nm-entry\ne-exit\n1-wall\n0-passage"
+ "Enter one line at a time; end with Ctrl-z:");
// String str = in.next();
try{
String str = buffer.readLine();
while (str != null) {
row++;
cols = str.length();
str = "1" + str + "1";
mazeRows.push(str);
if (str.indexOf(exitMarker) != -1) {
exitCell.x = row;
exitCell.y = str.indexOf(exitMarker);
} if (str.indexOf(entryMarker) != -1) {
entryCell.x = row;
entryCell.y = str.indexOf(entryMarker);
}
// str = in.next();
str = buffer.readLine();
}
} catch (IOException eof) {
}
rows = row;
store = new char[row + 2][];
store[0] = new char[cols + 2];
for (; !mazeRows.isEmpty(); row--) {
store[row] = ((String) mazeRows.pop()).toCharArray(); }
store[rows + 1] = new char[cols + 2];
for (col = 0; col <= cols + 1; col++) {
store[0][col] = wall;
store[rows + 1][col] = wall;
}
} public void display(PrintStream out) {
for (int row = 0; row <= rows + 1; row++) {
out.println(store[row]);
}
out.println();
} public void pushUnvisited(int row, int col) {
if (store[row][col] == passage || store[row][col] == exitMarker)
mazeStack.push(new MazeCell(row, col));
} public void exitMaze(PrintStream out) {
currentCell = entryCell;
out.println();
while (!currentCell.equals(exitCell)) {
int row = currentCell.x;
int col = currentCell.y;
display(System.out);
if (!currentCell.equals(entryCell))
store[row][col] = visited;
pushUnvisited(row - 1, col);
pushUnvisited(row + 1, col);
pushUnvisited(row, col - 1);
pushUnvisited(row, col + 1);
if (mazeStack.isEmpty()) {
display(out);
out.println("Failure");
return;
} else
currentCell = (MazeCell) mazeStack.pop();
}
display(out);
out.println("Success");
} public static void main(String[] args) {
(new Maze()).exitMaze(System.out);
}}{code}
以下是一次运行的过程及错误:
Enter a rectangular maze using the followingcharacter:
m-entry
e-exit
1-wall
0-passageEnter one line at a time; end with Ctrl-z:
1 1 0 0
0 0 0 e
0 0 m 1
111111111
11 1 0 01
10 0 0 e1
111111111Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at cn.mazeproject.Maze.pushUnvisited(Maze.java:72)
at cn.mazeproject.Maze.exitMaze(Maze.java:85)
at cn.mazeproject.Maze.main(Maze.java:101)
(new Maze()).exitMaze(System.out);
display(System.out); 这些里面的out是啥意思啊??
package cn.mazeproject; public class MazeCell {
public int x, y; public MazeCell() { } public MazeCell(int i, int j) {
x = i;
y = j;
} public boolean equals(MazeCell cell) {
return x == cell.x && y == cell.y;
}
} {code}
package cn.mazeproject; public class MazeCell {
public int x, y; public MazeCell() { } public MazeCell(int i, int j) {
x = i;
y = j;
} public boolean equals(MazeCell cell) {
return x == cell.x && y == cell.y;
}
} package cn.mazeproject; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Stack; public class Maze {
private int rows = 0, cols = 0;
private char[][] store;
private MazeCell currentCell, exitCell = new MazeCell(),
entryCell = new MazeCell();
private final char exitMarker = 'e', entryMarker = 'm', visited = '#';
private final char passage = '0', wall = '1';
private Stack <MazeCell> mazeStack = new Stack <MazeCell>(); public Maze() {
int row = 0, col = 0;
Stack <String> mazeRows = new Stack <String>();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(isr);
// Scanner in = new Scanner(System.in);
System.out.println("Enter a rectangular maze using the following"
+ "character:\nm-entry\ne-exit\n1-wall\n0-passage"
+ "Enter one line at a time; end with Ctrl-z:");
// String str = in.next();
try{
String str = buffer.readLine(); while (str != null) {
row++;
cols = str.length();
str = "1" + str + "1";
mazeRows.push(str);
if (str.indexOf(exitMarker) != -1) {
exitCell.x = row;
exitCell.y = str.indexOf(exitMarker);
} if (str.indexOf(entryMarker) != -1) {
entryCell.x = row;
entryCell.y = str.indexOf(entryMarker);
}
// str = in.next();
str = buffer.readLine();
}
} catch (IOException eof) {
}
rows = row;
store = new char[row + 2][];
store[0] = new char[cols + 2];
for (; !mazeRows.isEmpty(); row--) {
store[row] = ((String) mazeRows.pop()).toCharArray(); }
store[rows + 1] = new char[cols + 2];
for (col = 0; col <= cols + 1; col++) {
store[0][col] = wall;
store[rows + 1][col] = wall;
}
} public void display(PrintStream out) {
for (int row = 0; row <= rows + 1; row++) {
out.println(store[row]);
}
out.println();
} public void pushUnvisited(int row, int col) {
if (store[row][col] == passage ¦ ¦ store[row][col] == exitMarker)
mazeStack.push(new MazeCell(row, col));
} public void exitMaze(PrintStream out) {
currentCell = entryCell;
out.println();
while (!currentCell.equals(exitCell)) {
int row = currentCell.x;
int col = currentCell.y;
display(System.out);
if (!currentCell.equals(entryCell))
store[row][col] = visited;
pushUnvisited(row - 1, col);
pushUnvisited(row + 1, col);
pushUnvisited(row, col - 1);
pushUnvisited(row, col + 1);
if (mazeStack.isEmpty()) {
display(out);
out.println("Failure");
return;
} else
currentCell = (MazeCell) mazeStack.pop();
}
display(out);
out.println("Success");
} public static void main(String[] args) {
(new Maze()).exitMaze(System.out);
} }
以下是一次运行的过程及错误:
Enter a rectangular maze using the followingcharacter:
m-entry
e-exit
1-wall
0-passageEnter one line at a time; end with Ctrl-z:
1 1 0 0
0 0 0 e
0 0 m 1
111111111
11 1 0 01
10 0 0 e1
111111111 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at cn.mazeproject.Maze.pushUnvisited(Maze.java:72)
at cn.mazeproject.Maze.exitMaze(Maze.java:85)
at cn.mazeproject.Maze.main(Maze.java:101)