2 回答

TA貢獻1801條經(jīng)驗 獲得超8個贊
創(chuàng)造的想法mat很好。我不會費心去去掉第一行和最后一行,因為事實上,當你保留它們時,使用它們會更容易。這樣,i-1當您位于非墻壁位置時,行引用就不會超出范圍。
我也不會像w在那里存儲字符,而是存儲特定的數(shù)字,例如 -1 表示墻壁,0 表示免費。還為“A”和“B”存儲 0。當遇到這兩個字母時,您可以將它們的坐標存儲在特定變量中(例如rowA, colA, rowB, colB)。您可能需要檢查 B 是否位于 A 的右下方,否則肯定無法從 A 到達 B。
所以我將定義mat如下(請注意,我顛倒了尺寸,因為您的第二個示例表明輸入的第一行按該順序排列它們):
int[][] mat = new int[Integer.valueOf(dimensions[1])]
[Integer.valueOf(dimensions[0])];
int colA = mat[0].length;
int rowA = 0;
int colB = colA;
int rowB = 0;
for (int i = 0; i < mat.length; i++) {
String currLine = lines.get(i+1);
int j = 0;
for (char c : currLine.toCharArray()) {
mat[i][j] = c == '*' ? -1 : 0;
if (c == 'B') {
if (colA > j) return 0; // B unreachable from A
rowB = i;
colB = j;
} else if (c == 'A') {
if (colB < j) return 0; // B unreachable from A
rowA = i;
colA = j;
}
j++;
}
}
通過此設(shè)置,您可以重復(fù)mat存儲從 A 到當前位置的路徑數(shù)。值 0 atA應(yīng)設(shè)置為 1(從 A 到 A 僅有一條路徑),然后將上方和左側(cè)單元格中的值相加,確保 -1 被視為 0。
mat[rowA][colA] = 1;
for (int i = rowA; i <= rowB; i++) {
for (int j = colA; j <= colB; j++) {
if (mat[i][j] == 0) { // not a wall?
// count the number of paths that come from above,
// plus the number of paths that come from the left
mat[i][j] = Math.max(0, mat[i-1][j]) + Math.max(0, mat[i][j-1]);
}
}
}
return mat[rowB][colB]; // now this has the number of paths we are looking for
雖然遞歸方法也可以工作,但我建議使用上述動態(tài)編程方法,因為這樣可以避免多次重新計算某個單元的計數(shù)(當通過不同的 DFS 路徑到達那里時)。該解決方案具有線性時間復(fù)雜度。

TA貢獻1816條經(jīng)驗 獲得超4個贊
我建議使用 2 次調(diào)用進行簡單的遞歸:向下和向右。
這是代碼:
import java.io.File;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
public class JavaMazeInsideOfWallsAndGetAllPossiblePaths {
public static void main(String[] args) throws IOException, URISyntaxException {
Path mazePath = Paths.get( MethodHandles.lookup().lookupClass().getClassLoader()
.getResource("maze.txt").toURI());
File mazeFile = mazePath.toFile();
System.out.println(getPossiblePaths(mazeFile));
}
private static int getPossiblePaths(File f) throws IOException {
// 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];
int fromRow = -1, fromCol = -1, toRow = -1, toCol = -1;
for( int i = 2 ; i < lines.size() - 1 ; i++) {
String currLine = lines.get(i);
int j = 0;
for(char c : currLine.toCharArray()) {
switch(c) {
case '*':
continue; // for loop
case 'A':
mat[i-2][j] = 0;
fromRow = i-2;
fromCol = j;
break;
case 'B':
mat[i-2][j] = 2;
toRow = i-2;
toCol = j;
break;
default:
mat[i-2][j] = 1;
}
j++;
}
}
return getPossiblePathsRecursive(mat, fromRow, fromCol, toRow, toCol);
}
private static int getPossiblePathsRecursive(int[][] mat, int i, int j, int rows, int columns) throws IOException {
if(i > rows || j > columns) {
return 0;
}
if(mat[i][j] == 2) {
return 1;
}
return getPossiblePathsRecursive(mat, i+1, j, rows, columns) +
getPossiblePathsRecursive(mat, i, j + 1, rows, columns);
}
}
注意: 1. 跳過驗證步驟(假設(shè)輸入數(shù)據(jù)采用有效格式) 2. 忽略墻壁(假設(shè)始終有 4 堵墻 - 第一行、最后一行、第一列、最后一列。這些墻假設(shè)表示為“*”)
添加回答
舉報