| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 
 | class Solution {int[][] move = {{1,0}, {-1,0}, {0,1}, {0,-1}};
 boolean[][] visited;
 public boolean exist(char[][] board, String word) {
 visited = new boolean[board.length][board[0].length];
 for(int i = 0; i < board.length; i++){
 for(int j = 0; j < board[0].length; j++){
 if(dfs(board, word, i, j, 0)){
 return true;
 }
 }
 }
 return false;
 
 }
 public boolean dfs(char[][] board, String word, int x, int y, int k){
 if(x < 0 || x >= board.length || y < 0 || y >= board[0].length || visited[x][y]){
 return false;
 }
 if(board[x][y] != word.charAt(k)){
 return false;
 }else if(k == word.length()-1){
 return true;
 }
 visited[x][y] = true;
 for(int i = 0; i < 4; i++){
 int next_x = x + move[i][0];
 int next_y = y + move[i][1];
 if(dfs(board, word, next_x, next_y, k+1)){
 return true;
 }
 }
 visited[x][y] = false;
 return false;
 }
 }
 
 |