package maze;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Stack;class Maze{
int rows = 0, cols = 0;
char[][] store;
MazeCell currentCell, exitCell = new MazeCell(),
entryCell = new MazeCell();
final char exitMarker = 'e', entryMarker = 'm', visited = '.';
final char passage = '0', wall = '1';
Stack mazeStack = new Stack(); public Maze(){
int row = 0, col = 0;
Stack mazeRows = new Stack();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(isr);
String str;
System.out.println("Enter a rectangular maze using the following "
+ "characters:\nm - enter\ne - exit\n1 - wall\n0 - passage\n"
+ "Enter one line at time; end with Ctrl-Z");
try{
str = buffer.readLine();
//??????????????????????????????????????????
while(str != null){//?????死循环,怎么也跳不出???????????????
//??????????????????????????????????????????
row++;//行++
cols = str.length();
str = "1" + str + "1";//前后填补"1"
mazeRows.push(str);//串进栈
//System.out.println(mazeRows);
if(str.indexOf(exitMarker) != -1){//有出口
entryCell.x = row;
entryCell.y = str.indexOf(exitMarker);
}//System.out.println(entryCell);
if(str.indexOf(entryMarker) != -1){//有入口
exitCell.x = row;
exitCell.y = str.indexOf(entryMarker);
}//System.out.println(entryMarker);
str = buffer.readLine();
}
}
catch(IOException eof){
}
rows = row;
store = new char[rows + 2][];System.out.println(store);
store[0] = new char[cols + 2];System.out.println(store[0]);
for(; !mazeRows.isEmpty(); row--)//mazeRows不为空
store[row] = ((String) mazeRows.pop()).toCharArray();System.out.println(store[row]);
store[rows + 1] = new char[cols + 2];
for(col = 0; col <= cols + 1; col++){//左右补墙
store[0][col] = wall;
store[rows + 1][col] = wall;
}
// TODO Auto-generated constructor stub
} void display(PrintStream out){
for(int row = 0; row <= rows + 1; row++)
out.println(store[row]);//打印行
out.println();//换行
} void pushUnvisited(int row,int col){
if(store[row][col] == passage || store[row][col] == exitMarker)
mazeStack.push(new MazeCell(row,col));
} void exitMaze(PrintStream out){
int row = 0, col = 0;
currentCell = entryCell;
out.println();//换行
while(!currentCell.equals(exitCell)){//当前不为出口
row = currentCell.x;
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()){//mazeStack为空
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);
}
}
//----------------------------------------------------------------------------------------
/*
* Created on 2005-4-29
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package maze;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
class MazeCell{
int x, y; MazeCell(){
} MazeCell(int i,int j){
x = i;
y = j;
} boolean equals(MazeCell cell){
return x == cell.x && y == cell.y;
}
}
解决方案 »
- 数组和集合的区别
- jpcap 错误 问题 Unable to open a device: 10 devices are already opened.
- 发布Java应用程序
- JAVA高手进:接口本质上是类吗,接口和类有什么区别和联系,主要是联系,接口是否具备类的特征,接口是否是Object,
- 关于定义接口的问题
- n皇后的算法的补充程序,在线等
- 求教:Java程序为何突然全部无法运行
- 我的一个应用程序设置了MetalLookAndFeel,但是字体很难看,如何能够统一设置成我要的字体?
- 各位前辈,关于java命令的问题/
- @-@ @_@_@_@_@ 进来看看吧,300 分就是你的了!!!!!^-^-^-^-^
- 关于java环境配置的简单问题!
- 如何将一个较大的图像加载到一个面积较小的组件上,就是希望图像能按比例缩小,送分!!!!!!!
str = buffer.readLine();
while(str != null)只要不为空当然循环要进行下去了
while( (str = buffer.readLine()) != null){
...
我什么都不输入就按回车就应该是输入空串了吧??这样不行。while( (str = buffer.readLine()) != null){也不行。你们把代码复制进去试试。
while(!str.eaquals("e")){//?????死循环,怎么也跳不
+ "characters:\nm - enter\ne - exit\n1 - wall\n0 - passage\n"
+ "Enter one line at time; end with Ctrl-Z");用Ctrl-Z来结束输入,这个时候buffer.readLine()会返回 null
是输入 Ctrl-Z 然后回车马?不行
这是书上的程序。
是输入 Ctrl-Z 然后回车马?不行
这是书上的程序。