【发布时间】:2020-01-22 16:26:36
【问题描述】:
我知道这里还有很多其他的迷宫求解器。虽然我想有自己的方法,但我认为我的问题与其他人有点不同。
到目前为止,这就是我已经开始做的事情,希望我能实现我目前的想法。
private static int getPossiblePaths(File f) throws IOException {
int counts = 0; // hope to return all possible paths
// read input file then put it on list string
List<String> lines = Files.lines(f.toPath()).collect(Collectors.toList());
// get the row and column (dimensions)
String[] dimensions = lines.get(0).split(",");
//initalize sub matrix of the maze dimensions and ignoring the top and bottom walls
int[][] mat = new int[Integer.valueOf(dimensions[0]) - 2 ][Integer.valueOf(dimensions[1]) - 2];
//for each line in the maze excluding the boundaries (top and bottom)
for( int i = 2 ; i < lines.size() - 1 ; i++) {
String currLine = lines.get(i);
int j = 0;
for(char c : currLine.toCharArray()) {
mat[i-2][j] = (c=='*' ? 'w' : c=='A' ? 'a' : c=='B' ? 'b' : 's');
// some conditional statements here
}
}
// or maybe some conditional statements here outside of the loop
return counts;
}
文本文件中的迷宫是这样的。请注意,A 可以在任何地方并且与 B 相同。唯一允许的移动是向右和向下。
5,5
*****
*A *
* *
* B*
*****
上述迷宫的预期输出为 6(从 A 到 B 的可能路径)。
编辑:文本文件中的迷宫也可能是这样的:
8,5
********
* A *
* B*
* *
********
因此,使用我当前的代码,它正在获取尺寸(第一行)并移除迷宫的顶部和底部(边界)。因此 mat 数组中当前只存储了 3 行字符。以及文本文件中每个字符的一些编码(#=w(wall), A=a(start), B=b(end), else s(space))
我想在 foreach 中有一些条件语句来可能将每个字符存储在 ArrayList 中。虽然我不确定这种方法是否会让我的生活更加艰难。
你们的任何建议、提示、建议或其他更简单的方法将不胜感激!谢谢
【问题讨论】:
-
首先用 1 标记 end,它旁边的所有字段都用 2 标记...直到到达起点。然后每条最短路径总是向下 1。
-
第二个示例具有以列/行顺序指定的维度,但您的代码以相反的方式解释它们。此外,您的代码测试
#以查找墙壁,但您的输入示例使用*查找墙壁。
标签: java algorithm multidimensional-array text-files maze