我有 2 个对象。一种称为 Cell,一种称为 FinalSolve。该程序的目的是通过遍历每个单元格寻找出口来解决迷宫问题。
单元格对象代表迷宫中的单元格,并具有 4 个实例变量、1 个构造函数和 4 个返回指定实例变量的其他实例方法。
//the variables
private int value;
private int column;
private int row;
private char symbol;
private int nuValue;
//the methods
public value()
{
return value
}
等等。我的迷宫对象包含 6 个实例方法,如下所示:
- 解决
- 标记路径
- 找邻居
- findCellWithValue
- 设置值
他们有一个非常直截了当的名字,他们返回的正是我对他们的期望。
解决
这是使用算法并利用上面指定的所有方法尝试解决迷宫的方法
public void solve()
{
Cell[] entrance = findCellWithValue(250);
Cell[] neighborsOf = findNeighbors(entrance[0]);
for(Cell neighbor : neighborsOf)
{
setTheValue(1, neighbor);
}
int moveCount = 1;
boolean foundExit = false;
try{
while(!foundExit && moveCount <= 200)
{
Cell[] theCells = findCellWithValue(moveCount);
for(Cell justOne : theCells)
{
if(justOne.symbol() == '!')
{
foundExit = true;
markPath();
break;
}
else
{
Cell[] moreNeighbors = findNeighbors(justOne);
for(Cell prostie : moreNeighbors)
{
if(prostie.value() == 0)
{
setTheValue(moveCount+1, prostie);
}
}
}
}
moveCount++;
}
}catch(Exception e)
{
System.out.println("" + moveCount);
System.out.println("" + e.getMessage());
}
}
标记路径
一旦solve方法解决了程序,这个方法就会被调用,它会标记从出口到入口的路径。由于我在程序中遇到错误,此方法仍在进行中,它只打印“Hello”
public void markPath()
{
System.out.println("Hello");
}
找邻居
在迷宫中查找指定单元格的邻居(即空白)并将它们返回到数组中。
public Cell[] findNeighbors(Cell theCell)
{
Cell[] neighborsCell = new Cell[1];
int neighbors = 0;
int column = theCell.column();
int row = theCell.row();
if(column - 1 < 0);
else
{
char some = maze[column-1][row].symbol();
if(some == ' ')
{
if(neighbors == neighborsCell.length)
{
Cell[] biggerArray = new Cell[neighborsCell.length + 1];
System.arraycopy(neighborsCell, 0, biggerArray, 0, neighbors - 1);
neighborsCell = biggerArray;
}
neighborsCell[neighbors] = maze[column-1][row];
}
}
if(column + 1 > 20 );
else
{
char someElse = maze[column+1][row].symbol();
if(someElse == ' ')
{
if(neighbors == neighborsCell.length)
{
Cell[] biggerArray = new Cell[neighborsCell.length + 1];
System.arraycopy(neighborsCell, 0, biggerArray, 0, neighbors - 1);
neighborsCell = biggerArray;
}
neighborsCell[neighbors] = maze[column+1][row];
}
}
if(row - 1 < 0);
else
{
char someElse = maze[column][row-1].symbol();
if(someElse == ' ')
{
if(neighbors == neighborsCell.length)
{
Cell[] biggerArray = new Cell[neighborsCell.length + 1];
System.arraycopy(neighborsCell, 0, biggerArray, 0, neighbors - 1);
neighborsCell = biggerArray;
}
neighborsCell[neighbors] = maze[column][row-1];
}
}
if(row + 1 > 10);
else
{
char someElse = maze[column][row+1].symbol();
if(someElse == ' ')
{
if(neighbors == neighborsCell.length)
{
Cell[] biggerArray = new Cell[neighborsCell.length + 1];
System.arraycopy(neighborsCell, 0, biggerArray, 0, neighbors - 1);
neighborsCell = biggerArray;
}
neighborsCell[neighbors] = maze[column][row+1];
}
}
return neighborsCell;
}
覆盖默认的 toString 方法并返回从文件中读取时的迷宫
@Override
public String toString()
{
String result = "";
for(int row = 0; row < HEIGHT; row++)
{
for(int column = 0; column < WIDTH; column++) {
switch(maze[column][row].nuValue())
{
case HEDGE: result += HEDGE_REP; break;
case SPACE: result += SPACE_REP; break;
case ENTRANCE: result += ENTRANCE_REP; break;
case EXIT: result += EXIT_REP; break;
}
}//switch
result += NLS;
}//for
return result;
}//toString
findCellWithValue
循环整个迷宫以搜索具有作为参数的指定 int 值的单元格
public Cell[] findCellWithValue(int theValue)
{
int currentNoOfCells = 0;
Cell[] theCells = new Cell[INITIAL_ARRAY_SIZE];
for(int row = 0; row < HEIGHT; row++)
{
for(int column = 0; column < WIDTH; column++)
{
if(maze[column][row].value() == theValue)
{
if(currentNoOfCells == theCells.length)
{
Cell[] biggerArray = new Cell[theCells.length + ARRAY_RESIZE_FACTOR];
System.arraycopy(theCells, 0, biggerArray, 0, theCells.length);
theCells = biggerArray;
}
theCells[currentNoOfCells] = maze[column][row];
currentNoOfCells++;
}
}
}
return theCells;
}
设置值
将单元格的值设置为作为参数给出的指定 int
public void setTheValue(int value, Cell someCell)
{
int column = someCell.column();
int row = someCell.row();
char symbol = someCell.symbol();
maze[column][row] = new Cell(column, row, symbol, value);
}
我的解决方法中的 try and catch 语句纯粹是为了找出错误发生的原因和位置。当我得到 NullPointerException 时,它打印出 moveCount 为 6。
文件中的迷宫是一个 10 x 20 的矩形,周围有一个用“#”表示的树篱。空格表示为“”,退出为“!” 并通过“?”进入
也有人可能已经注意到,我的求解方法从找到 250 的单元格值开始。这是迷宫构造函数中给出的入口值,如下所示:
构造函数
public FinalSolve(Scanner input)
{
for(int row = 0; row < HEIGHT; row++)
{
String mazeLine = input.nextLine();
for(int column = 0; column < WIDTH; column++)
{
char character = mazeLine.charAt(column);
switch(character)
{
case SPACE_REP:
if(column == 7 && row == 7)
{
maze[column][row] = new Cell(column, row, SPACE_REP, 20);
}else{
maze[column][row] = new Cell(column, row, SPACE_REP, 0);
}
break;
case HEDGE_REP: maze[column][row] = new Cell(column, row, HEDGE_REP, 0);break;
case ENTRANCE_REP: maze[column][row] = new Cell(column, row, ENTRANCE_REP, 250);break;
case EXIT_REP: maze[column][row] = new Cell(column, row, EXIT_REP, 0);break;
}//switch
}//for
}//for
solve();
}//constructor
错误
方法求解中的第 51 行。这一行:
if(justOne.symbol() == '!')