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

``````输入：board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"

``````
``````示例 2：

``````

### 答案

``````
class Solution {
public boolean exist(char[][] board, String word) {

int  length1=board[0].length;//列数
int  length2=board.length;//行数

for(int i =0; i<length2;i++){
for(int j=0;j<length1;j++){
if(board[i][j]==word.charAt(0))
if(dfs(board,i,j,word,0))
return true;

}
}

return false;

}

public boolean dfs(char[][] board,int i, int j, String word, int u){

int  length1=board[0].length;//列数
int  length2=board.length;//行数
//失败:先判断边界值
if(i<0||i>=length2||j<0||j>=length1||board[i][j]!=word.charAt(u)){
return false;
}

//成功:遍历到最后一个
if(u==word.length()-1)
return true;
//覆盖原来防止重复
char temp = board[i][j];

board[i][j]= '#';
//递归调用

boolean res = dfs(board,i-1,j,word,u+1) || dfs(board,i+1,j,word,u+1) || dfs(board,i,j-1,word,u+1)
|| dfs(board,i,j+1,word,u+1) ;

//递归结束

board[i][j]= temp;

return res;

}

}

/*
charAt(int index)

*/

``````