-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
44 lines (33 loc) · 1.25 KB
/
Solution.java
1
2
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
37
38
39
40
41
42
43
44
class Solution1 {
public static void main(String[] args) {
System.out.println("Hello");
Solution1 o1 = new Solution1();
}
public boolean exist(char[][] board, String word) {
if (board == null || board[0].length == 0 || board.length == 0) {
return false;
}
int m = board.length;
int n = board[0].length;
boolean[][] visited = new boolean[m][n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (board[m][n] == word.charAt(0) && dfs(board, i, j, 0, visited)) {
return true;
}
}
}
return false;
}
public boolean dfs(char[][] board, int col, int row, int index, boolean[][] visited) {
if (row < 0 || col < 0 || row > board.length || col > board[0].length || visited[row][col]) {
return false;
}
visited[row][col] = true;
boolean res =
dfs(board, col + 1, row, index + 1, visited) || dfs(board, col, row + 1, index + 1, visited) ||
dfs(board, col - 1, row, index + 1, visited) ||dfs(board, col, row - 1, index + 1, visited);
visited[row][col] = false;
return res;
}
}