我正在从输入文件中读取迷宫大纲并将值存储在2D数组中。我能够在我的findPath()方法中找到退出,但我不确定如何从开始到完成跟踪实际路径。
我想将“Room”中的char值更改为“。”如果是在实际路径上。请告诉我如何才能做到这一点。任何帮助是极大的赞赏。谢谢。
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.*;
import javax.swing.JOptionPane;
public class Maze {
String inFile, // Name of file to be used as input
outFile, // Name of file to output completed maze to
line; // Current line being read by scanner
Room [][] rooms; // Holds the values that create maze
Room [] theStack;
Room goal;
Room current = new Room();
int rows, columns;
int tos = 0;
char [][] mazeC;
public static void main(String []args) throws Exception {
Maze m = new Maze();
}
public Maze() throws FileNotFoundException {
// Prompts user for the name of the file they wish to use as the input file.
inFile = JOptionPane.showInputDialog(null, "Please enter the name of the file you wish to read, including " + "the file path:");
if(inFile.equals("")) inFile = "C:/java/javaFiles/maze1.txt";
// Prompts user to enter the name they wish to save the file under.
outFile = JOptionPane.showInputDialog(null, "Please enter the filename you wish to save the data to:");
if(outFile.equals("")) outFile = "C:/java/javaFiles/mazeoutput1.txt";
// Creates a scanner object to read in the input file.
Scanner readFile = new Scanner(new FileReader(inFile));
PrintWriter output = new PrintWriter(outFile);
rows = readFile.nextInt();
columns = readFile.nextInt();
readFile.nextLine();
theStack = new Room[1000];
rooms = new Room [rows][columns];
for(int i = 0; i < rows; i++) {
line = readFile.nextLine();
for(int j = 0; j< line.length(); j++) {
Room r = new Room();
r.setValue(line.charAt(j));
if(r.getValue() == '$')
r.setGoal(true);
r.setBlocked(false);
if (r.getValue() == '*') {
r.setBlocked(true);
r.setGoal(false);
} else {
r.setGoal(false);
r.setBlocked(false);
}
r.setCord(i, j);
rooms[i][j] = r;
//mazeContent[i][j] = line.charAt(j);
}
}
createRooms();
findPath();
for(int i = 0; i < rows ; i++) {
for(int j = 0; j < columns ; j++) {
System.out.print(rooms[i][j].getValue());
}
System.out.println();
}
}
public void createRooms() {
for(int i = 1; i < rows - 1; i++) {
for(int j = 1; j < columns -1; j++) {
rooms[i][j].setCord(i,j);
rooms[i][j].setValue(rooms[i][j].getValue());
rooms[i][j].setUp(rooms, i, j);
rooms[i][j].setDown(rooms, i, j);
rooms[i][j].setRight(rooms, i, j);
rooms[i][j].setLeft(rooms, i, j);
if(rooms[i][j].getValue() == '*')
rooms[i][j].setBlocked(true);
else
rooms[i][j].setBlocked(false);
//System.out.println(rooms[i][j].getValue());
}
}
}
public void findPath() {
for(int i = 0; i < rows ; i++) {
for(int j = 0; j < columns ; j++) {
System.out.print(rooms[i][j].getValue());
}
System.out.println();
}
Room start = rooms[1][1];
push(start);
while(!isEmpty()) {
current = pop();
System.out.println("The value stored in current is" + current.getValue()+ "");
if (current == null)
System.out.println("current is null");
else if(current.getValue() == '$') {
System.out.println("Success");
current.setValue('f');
/*for (int i = 0; i < tos; i++) {
}*/
}
//System.out.println("The value is " + current.getValue());
else if(current.getBlocked() == false && current.getVisited() == false) {
System.out.println("pushing currents neighbors left, right....etc" +
"current is at" + current.getCord());
current.setVisited(true);
//
if(current.getRight() != null){
push(current.getRight()); System.out.println("Inside push 1" +current.getRight().getCord());}
else System.out.println("Inside push right is null");
if(current.getLeft() != null){
push(current.getLeft()); System.out.println("Inside push 2 " + current.getLeft().getCord());}
else System.out.println("Inside push left is null");
if(current.getUp() != null) {
push(current.getUp()); System.out.println("Inside push 3" + current.getUp().getCord());}
else System.out.println("Inside push up is null");
if(current.getDown() != null) {
push(current.getDown()); System.out.println("inside push 4" + current.getDown().getCord());}
else System.out.println("Inside push down is null");
}
}
}
public Room pop() {
// TODO Auto-generated method stub
System.out.println("I have Popped");
return theStack[--tos];
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return tos == 0;
}
public void push(Room item) {
System.out.println("I have pushed");
if (isFull()) {
System.out.println("The stack is full!");
}
else if(item == null){
System.out.println("you are pushing a null object");
}
else
System.out.println("stuff added");
theStack[tos++] = item;
}
public boolean isFull() {
return tos == theStack.length-1;
}
}
答案 0 :(得分:1)
我没有真正逐行完成你的代码,但这是一个解决方案:(我写了一个类似的程序,用于在图中找到Djikstra的最短路径)
在你的房间对象中创建一个名为 PREVIOUS 的属性。 每次去“下一个房间”时,请确保将下一个房间的 PREVIOUS 属性更改为您来自的房间。
当您到达目的地时:从目的地的 PREVIOUS 开始,然后穿过房间,直到找到没有 PREVIOUS 的房间,这将是你的来源(开始)房间。 即转到 PREVIOUS 会议室,将其添加到堆栈,转到 PREVIOUS 并重复此过程直到您到达起始室。< / p>
这应该有效,因为:如果你经历一个房间的路径而你遇到了一个死胡同,你将回溯并开始通过一条新路径。最终,您将以递归方式覆盖新路径中每个房间的 PREVIOUS 属性(每次)。当您到达目的地时,您将在当前(正确)路径中的每个房间中拥有最新(正确) PREVIOUS 属性。因此,当你走的时候,遍历并将每个房间的 PREVIOUS 添加到堆栈或队列或(无论你想要的话)。这应该是你的道路。