-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatch.java
41 lines (32 loc) · 1.23 KB
/
Match.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
public class Match {
public void exist(Board1 board1, String word, int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board1.get_data1(i, j) == word.charAt(0) && dfs(board1, i, j, 0, word)) {
System.out.println("word found: "+word);
return ;
}
}
}
// return false;
}
private boolean dfs(Board1 board1, int i, int j, int count, String word) {
if (count == word.length()) {
return true;
}
if (i < 0 || i >= board1.get_row1() || j < 0 || j >= board1.get_col1()) {
return false;
}
if (board1.get_data1(i, j) != word.charAt(count)) {
return false;
}
char temp = board1.get_data1(i, j);
board1.set_data1(i, j, '*');
boolean found = dfs(board1, i + 1, j, count + 1, word) ||
dfs(board1, i - 1, j, count + 1, word) ||
dfs(board1, i, j + 1, count + 1, word) ||
dfs(board1, i, j - 1, count + 1, word);
board1.set_data1(i, j, temp);
return found;
}
}