我一直在试图解决从文件中算出的迷宫(下面列出的迷宫)。这是我的代码,但它似乎并没有正确迭代,只是运行无穷大。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Scanner;
public class Maze {
private int exit;
private int entrance;
private ArrayList> maze;
/*Creates a Scanner for the inputed file with a maze. The input must be a file name and path (e.g. file.txt).
* In addition, this file must be formatted such that the length and height of the maze are on the first line,
* the second contains the array coordinates for the exit
* and the third the array coordinates for the entrance.
* The next lines should contain the maze drawn with 'x's and ' 's.
*/
public Maze (File file) throws FileNotFoundException
{
//innitialize maze
maze = new ArrayList>();
//print path of file
//System.out.println(file.getAbsolutePath());
Scanner readFile = new Scanner (file);
readFile.nextLine();
exit = readFile.nextInt();
System.out.println(exit);
readFile.nextLine();
entrance = readFile.nextInt();
readFile.nextLine();
System.out.println(entrance);
//int lineNumber = 0;
while (readFile.hasNextLine())
{
String line = readFile.nextLine();
ArrayList tempRow = new ArrayList();
for (int i = 0; i < line.length(); i++)
{
tempRow.add(line.charAt(i));
}
maze.add(tempRow);
//lineNumber++;
}
}
public void solvePrintMaze()
{
ArrayList> solvedMaze = solveMaze();
for (int i = 0; i < solvedMaze.size(); i++)
{
for (int j = 0; j < solvedMaze.get(i).size(); j++)
System.out.print(solvedMaze.get(i).get(j));
System.out.print("\n");
}
}
/*solveMaze returns a the solved maze, with 'R's replacing spaces to represent the route taken through the maze.
* If it cannot solve the maze, it returns the original maze. If the program makes 1000 moves and does not solve the maze,
* the maze is considered unsolvable and the original maze is returned. If no move can be made, the maze is also considered unsolvable.
*/
public ArrayList> solveMaze()
{
ArrayList> workedMaze = maze;
ArrayList> solvedMaze = maze;
int row = maze.size() - 1;
int column = entrance;
int moves = 0;
while (!(row == 0 && column == exit))
{
System.out.println(row);
System.out.println(column);
solvedMaze.get(row).set(column, 'R');
ArrayList tempRowSolved = solvedMaze.get(row);
tempRowSolved.set(column, 'R');
ArrayList tempRowWorked = workedMaze.get(row);
if (isDeadEnd(row, column, workedMaze))
{
tempRowWorked.set(column, 'x');
}
workedMaze.set(row, tempRowWorked);
System.out.println(workedMaze);
solvedMaze.set(row, tempRowSolved);
/*if (column == nextColumn(row, column, workedMaze) && row == nextRow(row, column, workedMaze)) {
return maze;
} */
column = nextColumn(row, column, workedMaze);
row = nextRow(row, column, workedMaze);
//System.out.println(moves);
/*if (moves == 1000)
return maze;
moves ++; */
}
return solvedMaze;
}
/*Takes in the row and column numbers in the array (if they exist in it) and returns true only if that space is available to move to
* (The space exists and is not an 'x').
*/
public static boolean canMove (int row, int column,ArrayList> arrayMaze)
{
if (column < arrayMaze.size() && column >=0 && row < arrayMaze.get(row).size() && row >= 0 && arrayMaze.get(row).get(column).equals( ' '))
{
return true;
}
return false;
}
/*The next four methods use canMove to determine if the space below, above, to the left, and to the right respectively can be moved to.
*/
public static boolean canMoveBot (int row, int column, ArrayList> arrayMaze)
{
return canMove(row+1, column, arrayMaze);
}
public static boolean canMoveTop (int row, int column,ArrayList> arrayMaze)
{
return canMove(row-1, column, arrayMaze);
}
public static boolean canMoveLeft (int row, int column, ArrayList> arrayMaze)
{
return canMove(row, column -1, arrayMaze);
}
public static boolean canMoveRight (int row, int column, ArrayList> arrayMaze)
{
return canMove(row, column + 1, arrayMaze);
}
/*Uses the canMove methods to determine if three directions around the current position cannot be moved to.
*/
public static boolean isDeadEnd (int row, int column, ArrayList> arrayMaze)
{
int n = 0;
if (!(canMoveBot(row, column, arrayMaze)))
n++;
if (!(canMoveBot(row, column, arrayMaze)))
n++;
if (!(canMoveBot(row, column, arrayMaze)))
n++;
if (!(canMoveBot(row, column, arrayMaze)))
n++;
if (n >= 3)
return true;
return false;
}
/* This uses the canMove methods to determine where to move next. If there are multiple possibilities, it checks and chooses a direction
* in the order of going up, then down.
*/
public int nextRow(int row, int column, ArrayList> arrayMaze)
{
if (canMoveTop(row, column, arrayMaze))
return row - 1;
if (canMoveBot(row, column, arrayMaze))
return row + 1;
return row;
}
/* This works like nextRow, but it returns the column amount, choosing to go right first if there are multiple ways to go
*/
public int nextColumn(int row, int column, ArrayList> arrayMaze)
{
if (canMoveRight(row, column, arrayMaze))
return column + 1;
if (canMoveLeft(row, column, arrayMaze))
return column - 1;
return column;
}
/**
* @param args
*/
public static void main(String[] args) {
File mazeFile = new File ("maze1.txt");
Maze testMaze = null;
try {
testMaze = new Maze(mazeFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (testMaze!= null){
testMaze.solvePrintMaze();
}
else
System.out.println("Maze file cannot be found");
}
}这是我将要从中输入的.txt文件。
20 7
18 0
12 6
xxxxxxxxxxxxxxxxxx x
x x xxxx x
x xxxxx xxxxx xx x
x xxxxx xxxxxxx xx x
x xx xx x
x xxxxxxxxxx xx x
xxxxxxxxxxxx xxxxxxx