-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTest2.java
61 lines (57 loc) · 1.66 KB
/
Test2.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
60
61
public class Test2 {
public static boolean exist(char[][] board, String word) {
int row = board.length;
char[] w = word.toCharArray();
if (row < 1) {
return false;
}
int col = board[0].length;
boolean[][] flag = new boolean[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
flag[i][j] = true;
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == word.charAt(0)) {
flag[i][j] = false;
if (wheExists(board, i, j, word.substring(1), flag)) {
return true;
}
flag[i][j] = true;
}
}
}
return false;
}
public static boolean wheExists(char[][] board, int i, int j, String w, boolean[][] flag) {
if (0 == w.length()) {
return true;
}
// 分别代表board中元素上 下 左 右移动
int[][] direction = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
for (int k = 0; k < direction.length; k++) {
int ii = i + direction[k][0];
int jj = j + direction[k][1];
if (ii >= 0 && ii < board.length && jj >= 0 && jj < board[i].length && board[ii][jj] == w.charAt(0)
&& flag[ii][jj]) {
flag[ii][jj] = false;
if (w.length() == 1 || wheExists(board, ii, jj, w.substring(1), flag)) {
return true;
}
flag[ii][jj] = true;
}
}
return false;
}
public static void main(String[] args) {
char[][] a = { { 'A', 'B', 'C', 'E' }, { 'S', 'F', 'C', 'S' }, { 'A', 'D', 'E', 'E' } };
String b = "ABCESCED";
long x = System.currentTimeMillis();
System.out.println(exist(a, b));
long y = System.currentTimeMillis();
System.out.println(y - x);
System.out.println(b.substring(1));
}
}