写法一

``````class Solution {
boolean[][] vis;

public boolean exist(char[][] board, String word) {
int m = board.length, n = board[0].length;
vis = new boolean[m][n];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(dfs(board, i, j, word, 0)) return true;
}
}
return false;
}

boolean dfs(char[][] board, int x, int y, String word, int ind){
if(x < 0 || x >= board.length || y < 0 || y >= board[0].length || vis[x][y] || board[x][y] != word.charAt(ind)) return false;
if(ind == word.length() - 1) return true;

vis[x][y] = true;
if(dfs(board, x + 1, y, word, ind + 1)) return true;
if(dfs(board, x - 1, y, word, ind + 1)) return true;
if(dfs(board, x, y + 1, word, ind + 1)) return true;
if(dfs(board, x, y - 1, word, ind + 1)) return true;
vis[x][y] = false;

return false;
}
}``````

写法二

for循环

``````class Solution {
int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
boolean[][] vis;

public boolean exist(char[][] board, String word) {
int m = board.length, n = board[0].length;
vis = new boolean[m][n];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(dfs(board, i, j, word, 0)) return true;
}
}
return false;
}

boolean dfs(char[][] board, int x, int y, String word, int ind){
int m = board.length, n = board[0].length;
if(x < 0 || x >= m || y < 0 || y >= n || vis[x][y] || board[x][y] != word.charAt(ind)) return false;
if(ind == word.length() - 1) return true;

vis[x][y] = true;
for(int i = 0; i < dir.length; i++){
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if(dfs(board, nx, ny, word, ind + 1)) return true;
}
vis[x][y] = false;

return false;
}
}``````

之前写法

解法一

``````class Solution {
int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
boolean[][] vis;

public boolean exist(char[][] board, String word) {
int m = board.length, n = board[0].length;
vis = new boolean[m][n];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(backtrack(board, i, j, word, 0)) return true;
}
}
return false;
}

boolean backtrack(char[][] board, int x, int y, String word, int ind){
int m = board.length, n = board[0].length;
if(board[x][y] != word.charAt(ind)) return false;
if(ind == word.length() - 1) return true;

vis[x][y] = true;
for(int i = 0; i < dir.length; i++){
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if(nx < 0 || nx >= m || ny < 0 || ny >= n || vis[nx][ny]) continue;
if(backtrack(board, nx, ny, word, ind + 1)) return true;
}
vis[x][y] = false;

return false;
}
}``````

解法二

``````class Solution {
int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
boolean[][] vis;

public boolean exist(char[][] board, String word) {
int m = board.length, n = board[0].length;
vis = new boolean[m][n];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
vis[i][j] = true;
if(backtrack(board, i, j, word, 0)) return true;
vis[i][j] = false;
}
}
return false;
}

boolean backtrack(char[][] board, int x, int y, String word, int ind){
int m = board.length, n = board[0].length;
if(board[x][y] != word.charAt(ind)) return false;
if(ind == word.length() - 1) return true;

for(int k = 0; k < dir.length; k++){
int nx = x + dir[k][0];
int ny = y + dir[k][1];
if(nx < 0 || nx >= m || ny < 0 || ny >= n || vis[nx][ny]) continue;
vis[nx][ny] = true;
if(backtrack(board, nx, ny, word, ind + 1)) return true;
vis[nx][ny] = false;
}

return false;
}
}``````