leetcode算法练习:矩阵中的路径

面试题12. 矩阵中的路径
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左、右、上、下移动一格。如果一条路径经过了矩阵的某一格,那么该路径不能再次进入该格子。例如,在下面的3×4的矩阵中包含一条字符串“bfce”的路径(路径中的字母用加粗标出)。

[["a","b","c","e"],
["s","f","c","s"],
["a","d","e","e"]]

但矩阵中不包含字符串“abfb”的路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入这个格子。

 

示例 1:

输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
输出:true
示例 2:

输入:board = [["a","b"],["c","d"]], word = "abcd"
输出:false

思路:
对二维数组进行遍历,得到一个值先判断是否等于等于word[0]如果等于进入递归函数,递归函数判断当前值的上下左右的值是否等于word+1,如果等于则令当前值等于*然后将下一个值进行递归,如果在递归途中发生断路则要让变成 *的值编回来。
代码:

bool emmm(char** board, int boardSize, int* boardColSize, char* word,int j,int i){
    char swap=board[i][j];
    
  
    if(*word==NULL)
        return 1;

   if(i>0){printf("上");//上
      
       if(board[i-1][j]==*word){
       
           board[i][j]='*';
           if(emmm(board,boardSize,boardColSize,word+1,j,i-1))
                return 1;
            else
                board[i][j]=swap;

       }
   }
   if(j>0){

       if(board[i][j-1]==*word){
      
           board[i][j]='*';
           if(emmm(board,boardSize,boardColSize,word+1,j-1,i))
                return 1;
            else
                board[i][j]=swap;
       }
            
   }

   if(j<*boardColSize-1){//右
   
       if(board[i][j+1]==*word){
    
           board[i][j]='*';
           if(emmm(board,boardSize,boardColSize,word+1,j+1,i))
                return 1;
            else
                board[i][j]=swap;
       }
            
   }
   if(i<boardSize-1){//下
 
       if(board[i+1][j]==*word)
       {
        
           board[i][j]='*';
           if(emmm(board,boardSize,boardColSize,word+1,j,i+1))
                return 1;
            else
                board[i][j]=swap;
       }
            
   }
return 0;
}
bool exist(char** board, int boardSize, int* boardColSize, char* word){


for(int i=0;i<boardSize;i++){//i是行
    for(int j=0;j<*boardColSize;j++){//j是列

        if(board[i][j]==*word){
                
        if(emmm(board,boardSize,boardColSize,word+1,j,i))
            return 1;
        }
      
    }
}

return 0;
}

官方答案:

int dfs (char** board,int boardSize, int* boardColSize, char* word, int i, int j, int k) {
    if (i < 0 || i >= boardSize || j < 0 || j >= boardColSize[i] || board[i][j] != word[k] ) {
        return false;
    }
    if (word[k+1] == '\0') {
        return true;
    }

    char tmp = board[i][j];
    board[i][j] = '/';

    int ret =( dfs(board, boardSize, boardColSize,word,i+1,j,k+1) 
            || dfs(board, boardSize, boardColSize,word,i-1,j,k+1)
            || dfs(board, boardSize, boardColSize,word,i,j+1,k+1) 
            || dfs(board, boardSize, boardColSize,word,i,j-1,k+1) );

    board[i][j] = tmp;
    return ret;
}

bool exist(char** board, int boardSize, int* boardColSize, char* word){
    int i,j;
    int ret;

    for (i = 0; i < boardSize; i++) {
        for (j = 0; j < boardColSize[i]; j++) {
            if (dfs(board,boardSize,boardColSize,word,i,j,0)) {
                return true;
            }
        }
    } 
    
    return false;
}

你可能感兴趣的:(算法与数据结构)