1. 程式人生 > 實用技巧 >[程式設計題] JZ65 矩陣中的路徑

[程式設計題] JZ65 矩陣中的路徑

[程式設計題] JZ65 矩陣中的路徑

題目描述

輸入輸出案例

參考

參考

思路

使用回溯、遞迴、列舉的思想

程式碼

class Solution {
    public boolean exist(char[][] board, String word) {
        //用於標記是否已經被訪問的二維陣列
        boolean[][] isSearched = new boolean[board.length][board[0].length];


        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if(solve(board,word,i,j,isSearched,0)){
                    return true;
                }
            }
        }
        //執行到此處,返回false
        return false;
    }

    private boolean solve(char[][] board, String word, int i, int j, boolean[][] isSearched, int i1) {
        int index = i1;
        //遞迴的退出條件(邊界判斷)
        if(i<0 || i >=board.length || j<0 || j>=board[0].length || isSearched[i][j]){
            return false;
        }
        //匹配到某一位置不滿足退出
        if(word.charAt(index)!=board[i][j]){
            return false;
        }
        //成功匹配到字串末尾
        if (index==word.length()-1){
            return true;
        }

        //本次訪問就把[i,j]位置標誌為已訪問
        isSearched[i][j] = true;
        //遞迴體
        boolean flag = solve(board,word,i+1,j,isSearched,index+1) ||    //注意這裡要寫index+1 ,不能每個都寫index++;
                       solve(board,word,i-1,j,isSearched,index+1) ||
                       solve(board,word,i,j-1,isSearched,index+1) ||
                       solve(board,word,i,j+1,isSearched,index+1);

        //訪問過程,重新恢復訪問標誌。(回溯思想)
        isSearched[i][j] = false;
        
        return flag;
    }
}

牛客的輸入格式

Java程式碼

public class Solution {
    public boolean hasPath(char[] matrix, int rows, int cols, char[] str){
        //這裡把一維陣列變為二維陣列
        char[][] board = new char[rows][cols];
        //一維陣列轉二維
        int index = 0;
        int row_ = 0;
        for(int i=0;i<matrix.length;i++){
            if(index>=cols){
                index = 0; //索引置為0
                row_++;//換下一行
            }
            board[row_][index] = matrix[i];
            index++;
        }
        //char[] 陣列變字串
        String word = String.valueOf(str);
        
        //呼叫
        return exist(board,word);
        
    }
    
    //力扣的輸入
    public boolean exist(char[][] board, String word) {
        //用於標記是否已經被訪問的二維陣列
        boolean[][] isSearched = new boolean[board.length][board[0].length];


        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if(solve(board,word,i,j,isSearched,0)){
                    return true;
                }
            }
        }
        //執行到此處,返回false
        return false;
    }

    private boolean solve(char[][] board, String word, int i, int j, boolean[][] isSearched, int i1) {
        int index = i1;
        //遞迴的退出條件(邊界判斷)
        if(i<0 || i >=board.length || j<0 || j>=board[0].length || isSearched[i][j]){
            return false;
        }
        //匹配到某一位置不滿足退出
        if(word.charAt(index)!=board[i][j]){
            return false;
        }
        //成功匹配到字串末尾
        if (index==word.length()-1){
            return true;
        }

        //本次訪問就把[i,j]位置標誌為已訪問
        isSearched[i][j] = true;
        //遞迴體
        boolean flag = solve(board,word,i+1,j,isSearched,index+1) ||    //注意這裡要寫index+1 ,不能每個都寫index++;
                       solve(board,word,i-1,j,isSearched,index+1) ||
                       solve(board,word,i,j-1,isSearched,index+1) ||
                       solve(board,word,i,j+1,isSearched,index+1);

        //訪問過程,重新恢復訪問標誌。(回溯思想)
        isSearched[i][j] = false;
        
        return flag;
    }

}