-
Notifications
You must be signed in to change notification settings - Fork 65
/
矩阵中的路径.js
62 lines (57 loc) · 1.57 KB
/
矩阵中的路径.js
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
62
/**
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*/
var exist = function(board, word) {
let ans = false;
const row = board.length;
const col = board[0].length;
const end = word.length - 1;
const visited = new Array(row).fill(0).map(() => new Array(col).fill(false));
const ways = [
[-1, 0],
[0, 1],
[1, 0],
[0, -1]
];
const overBorder = (i, j) => {
return i < 0 || i >= row || j < 0 || j >= col;
}
// k 索引
const dfs = (i, j, k) => {
// 1. 确定是否找到答案
// 2. 是否终止
if (k > end || ans) return;
if (k === end) {
return ans = true
}
// 3. 继续搜
for (const way of ways) {
let [si, sj] = way;
const ni = i + si;
const nj = j + sj;
if (overBorder(ni, nj)) continue;
if (board[ni][nj] !== word[k + 1]) continue;
if (visited[ni][nj]) continue;
visited[ni][nj] = true;
dfs(ni, nj, k + 1);
visited[ni][nj] = false;
}
}
// 每一格尝试,四个方向搜索
outer: for (let i = 0; i < row; i++) {
for (let j = 0; j < col; j++) {
if (ans) {
break outer;
}
// 当前值 === 第一个字母
if (board[i][j] === word[0]) {
visited[i][j] = true;
dfs(i, j, 0); // board[j][j] === word[0]
visited[i][j] = false;
}
}
}
return ans;
};