示例迷宫:
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW
WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW
WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOOOOW
WOOOOOOWWWWWWWWWWWWWWOOOOOOOOOOOWWWWWWWWOOOOOOOOOOOOOOOWWW
WOOOOWWWWWWWOOOOOOWWWWOOOOOOWWWWWWWWWWWOOOOWWWWWWWWWOWWWWW
WOOOWWWWWWWWWWWWOOWWWWWWWWWWWWOOOOOOOOOOOOWWWWWWWWWOOOOOWW
WOOWWWWWWWWWWWWWOOWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWWOOOW
WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW
WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW
WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWOFW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
我们需要像上面这样接收一个 txt 文件,如果它是可解决的,则使用堆栈返回 true 或 false。
我的问题是:如何从文件中逐个获取字符,并将它们指定为 2D 数组中的点以推送到堆栈中?我在下面有一些伪代码...
这是我所拥有的...
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
import java.awt.Point;
public class MazeExplorer {
public static int x;
public static int y;
final int mazeHeight = 12;
final int mazeWidth = 58;
public static char[][] mazeLocationPoints = new char[12][58];
public static void main(String[] args) throws FileNotFoundException{
File f = new File("Maze1.txt");
Scanner sc = new Scanner(f);
String mazeString = new Scanner( f ).useDelimiter("\A").next();
Stack<Point> points = new Stack<>();
while(sc.hasNextLine()){
mazeLocationPoints[][] = sc.nextLine().toCharArray();
points.push(mazeLocations);
}
}
}
我不知道如何在迷宫位置中创建点..?
给定的迷宫是 12 x 58,我已经转换为字符串,但是我如何为每个位置(字符)分配一个点 (x, y) 值? 我知道底部是错误的,这只是为了展示我想做什么。
遍历维度:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
import java.awt.Point;
public class MazeExplorer {
public static int x;
public static int y;
final static int mazeHeight = 12;
final static int mazeWidth = 58;
public static char[][] mazePoints = new char[12][58];
public static void main(String[] args) throws FileNotFoundException{
File f = new File("Maze1.txt");
Scanner sc = new Scanner(f);
String mazeString = new Scanner( f ).useDelimiter("\A").next();
Stack<Point> points = new Stack<>();
for(int i = 0; i < mazeHeight; i++){
for(int j = 0; j < mazeWidth; j++){
mazePoints[i][j] =
}
}
}
}
首先,你应该使用你声明的静态整数初始化你的mazePoints
数组:
public static char[][] mazePoints = new char[mazeHeight][mazeWidth];
但是您需要先将它们标记为静态:
private static final int mazeHeight = 12;
private static final int mazeWidth = 58;
如果它们不是静态的,那么您的mazePoints
数组也不应该是静态的。
接下来,您应该逐行读取文件,而不是一次读取整个内容:
final Scanner scanner = new Scanner(f);
for (int row = 0; row < mazeHeight && scanner.hasNext(); row++) {
final String mazeRow = scanner.next(); //Get the next row from the scanner.
mazePoints[row] = mazeRow.toCharArray(); //Convert the row into a char[].
}
大功告成。你现在把所有的观点都mazePoints
了。
请注意,您使用new Scanner(f).useDelimiter("\A").next();
只是更改默认分隔符,以便它一次读取整个文件。你不想要这种行为。使用一次读取一行的默认行为。
您可以盲目遍历整个 2D 数组,如下所示,但是,您可能需要实现某种类型的算法,而不是迭代整个 2D 数组。
for (int y = 0; y < mazeHeight; y++) {
for (int x = 0; x < mazeWidth; x++) {
Point point = new Point(x, y);
//Do whatever with your point...
}
}