您好我是stackoverflow和Java的新手。我已经在这个项目上工作了很长一段时间(大约一周),我需要创建一个从每个文本字段的[0,0]到[9,9]运行的迷宫求解器(idk如果我' m使用正确的术语)。每个迷宫由一条空行分隔。我有正确显示迷宫的程序,但现在问题出现了,我无法解决每个迷宫,只有文本文件中的最后一个迷宫,即使我改变了迷宫的顺序,它只是解决了最后一个。程序运行迷宫中的最后一个迷宫,但是我无法从第一个迷宫开始并连续执行每个迷宫,这是我工作的代码:
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
public class Maze
{
private static char[][] maze;
private static ArrayList<String> mazegenerator;
public static void initializeMaze(String fileName)
{
try
{
mazegenerator = new ArrayList<>();
int numcols = 0;
int r = 0;
Scanner file = new Scanner(new File(fileName));
while(file.hasNextLine())
{
String nextLine = file.nextLine(); // sets the String nextLine a value
mazegenerator.add(nextLine); // inserts the string into the array
if (nextLine.length() > numcols)
numcols = nextLine.length(); //sets numcols to the length of the Line
if(nextLine.length() == 0)
clearthismaze(); // clears the input from array
} // loop to collect the cols and set the row size
int numrows = mazegenerator.size();
maze = new char[numrows][numcols];
for (int i ; r < numrows; r ++)
{
String row = mazegenerator.get(r);
for (int c = 0; c < numcols; c++)
{
if(row.length() >= c)
maze[r][c]= row.charAt(c);
}
}
if(r == 10 && numrows == 10 && numcols == 10)
System.out.println();
printMaze();
solveMaze(0,0);
System.out.println();
printMaze();
numrows = 0;
numcols = 0;
}
catch(Exception e){System.out.println(fileName + " has an issue");}
} // this method generates the maze and sets it up which is later printed
public static void printMaze()
{
for (char[] row: maze)
{
for (char c: row)
System.out.print(c);
System.out.println();
}
System.out.println();
} //method to print maze accordingly
public static void main (String[] args)
{
System.out.println("This program prints the original maze before solving and then prints the maze solution, \n"
+ "solving from the upper left to the lower right. X's represent routes taken, 2's represent the true path, \n"
+ "if no 2's are present then that means the maze is unsolvable \n");
initializeMaze("multiplemazes.txt");
} // main method , driver method
public static boolean solveMaze(int r, int c)
{
boolean finished = false;
if(PossibleToMove(r,c)){
maze[r][c] = 'X'; // coordinate has been visited mark it so you don't return to it
if(c == maze[0].length - 1 && r == maze.length-1)// base case: did I reach destination?
finished = true; // maze is solved
else{
if(!finished)
finished = solveMaze(r,c-2); // look left
if(!finished)
finished = solveMaze(r-1,c); // look up
if(!finished)
finished = solveMaze(r,c+2); // look right
if(!finished)
finished = solveMaze(r+1,c); // look down
}
if(finished) // part of the true path
maze[r][c] = '2';
if(!finished){
System.out.println(">>>" + " Backtracking from " + "( " + r + ", " + c/2 + ")");
}
}
// if I know that I reached a dead end
// this can't be part of the solution
return finished;
} //method solveMaze
private static boolean PossibleToMove(int r, int c){
boolean canmove = false;
if(r >=0 && r < maze.length && c >=0 && c < maze[0].length)
if(maze[r][c] == '0')
canmove = true;
return canmove;
} // method PossibleToMove
public static void clearthismaze(){
mazegenerator.clear();
}
} // class Maze
以下是4个迷宫的文本文件示例:
0 1 0 0 0 0 0 0 0 1
0 0 0 1 0 1 0 1 0 1
0 1 0 1 0 1 1 1 1 1
1 0 0 1 0 0 0 1 0 0
1 0 1 1 0 1 0 1 0 1
1 0 0 0 1 1 0 1 0 0
0 0 1 0 0 0 0 1 1 0
0 1 1 0 1 1 0 0 0 0
1 1 0 1 1 1 0 1 0 1
1 0 0 0 0 0 0 1 0 0
0 0 1 0 0 0 1 1 0 1
1 0 0 1 0 1 0 0 0 1
0 1 0 0 0 1 1 1 0 1
0 0 1 1 0 0 0 1 0 0
1 0 0 1 1 1 1 1 0 1
1 0 0 1 0 1 1 0 0 1
0 0 1 0 1 0 0 1 1 1
0 1 1 0 0 0 0 0 1 1
0 1 0 1 1 1 0 0 0 0
0 0 0 1 1 0 0 1 1 0
0 0 0 0 1 1 0 1 0 0
0 1 1 0 0 0 0 1 1 0
0 1 1 0 1 1 0 0 0 0
1 1 0 1 1 1 0 1 0 1
1 0 0 0 0 0 0 1 0 0
0 1 0 1 0 1 0 0 0 1
0 0 0 1 0 1 0 1 0 1
0 1 0 1 0 1 1 1 1 1
1 0 1 1 0 1 0 0 0 1
1 0 0 1 0 0 0 1 0 0
0 0 0 0 1 1 0 1 0 0
0 1 1 0 0 0 0 1 1 0
0 1 1 0 1 1 0 0 0 0
1 1 0 1 1 1 0 1 0 1
1 0 0 0 1 0 0 1 0 0
0 1 0 1 0 1 0 0 0 1
0 0 0 1 0 1 0 1 0 1
0 1 0 1 0 1 1 1 1 1
1 0 1 1 0 1 0 0 0 1
1 0 0 1 0 0 0 1 0 0
任何数量的帮助都是有用的,是的,这是一个家庭作业,我想我只是陷入循环问题,或者我的clearthismaze()方法有问题。我的意图是打印出数组,解析数组,清除数组,跳过空行然后移动到下一个数组。如果有人能够确定问题我会非常感激,谢谢。
答案 0 :(得分:0)
如果您获得空行,则清除mazegenerator
。因此,如果您开始解决迷宫,只有最后一个可用。
您可以将类型更改为List<List<String>> mazegenerator
。所以mazegenerator.get(0)
拥有第一个迷宫,mazegenerator.get(0).get(0)
拥有第一个迷宫的第一行。
您还需要更改文件读取循环:
List<String> singleMaze = new ArrayList<>();
while(file.hasNextLine()) {
// ...
if(nextLine.length() == 0) {
mazegenerator.add(singleMaze);
singleMaze = new ArrayList<>();
}
}