第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java迷宮墻內(nèi)并獲得所有可能的路徑

Java迷宮墻內(nèi)并獲得所有可能的路徑

Qyouu 2023-08-16 17:27:07
我知道這里還有很多其他迷宮求解器。雖然我想有自己的方法,但我認為我的問題與其他人有點不同。到目前為止,這就是我已經(jīng)開始的事情,希望我能實現(xiàn)我現(xiàn)在的想法。    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******上述迷宮的預(yù)期輸出是 6(從 A 到 B 的可能路徑)。編輯:文本文件中的迷宮也可能是這樣的:8,5********* A    **     B**      *********因此,使用我當前的代碼,它獲取尺寸(第一行)并刪除迷宮的頂部和底部部分(邊界)。所以目前mat數(shù)組中只存儲了 3 行字符。以及文本文件中每個字符的一些編碼(#=w(wall), A=a(start), B=b(end), else s(space))我想在 foreach 內(nèi)部有一些條件語句,以便可能將每個字符存儲在 ArrayList 中。盡管我不確定這種方法是否會讓我的生活變得更加困難。如果你們有任何建議、技巧、建議或其他更簡單的方法,我們將不勝感激!謝謝
查看完整描述

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ù)雜度。


查看完整回答
反對 回復(fù) 2023-08-16
?
繁華開滿天機

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è)表示為“*”)


查看完整回答
反對 回復(fù) 2023-08-16
  • 2 回答
  • 0 關(guān)注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號