-
Notifications
You must be signed in to change notification settings - Fork 6
/
WordSearch.java
59 lines (58 loc) · 1.72 KB
/
WordSearch.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package io.ziheng.recursion.backtracking.leetcode;
/**
* LeetCode 79. Word Search
* https://leetcode.com/problems/word-search/
*/
public class WordSearch {
private int colNum;
private int rowNum;
private boolean[][] visited;
private int[][] distances = {
{ 1, 0, },
{-1, 0, },
{ 0, 1, },
{ 0, -1, },
};
public boolean exist(char[][] board, String word) {
if (board == null || board.length == 0
|| board[0] == null || board[0].length == 0
|| word == null || word.length() == 0) {
return false;
}
rowNum = board.length;
colNum = board[0].length;
visited = new boolean[rowNum][colNum];
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < colNum; j++) {
if (board[i][j] == word.charAt(0) && dfs(board, word, i, j, 0)) {
return true;
}
}
}
return false;
}
private boolean dfs(char[][] board, String word, int i, int j, int index) {
if (index == word.length()) {
return true;
}
if (!inArea(i, j)
|| visited[i][j]
|| board[i][j] != word.charAt(index)) {
return false;
}
visited[i][j] = true;
for (int k = 0; k < distances.length; k++) {
int nextX = i + distances[k][0];
int nextY = j + distances[k][1];
if (dfs(board, word, nextX, nextY, index + 1)) {
return true;
}
}
visited[i][j] = false;
return false;
}
private boolean inArea(int x, int y) {
return x >= 0 && x < rowNum && y >= 0 && y < colNum;
}
}
/* EOF */