forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
58 lines (55 loc) · 1.3 KB
/
Copy pathmain.js
File metadata and controls
58 lines (55 loc) · 1.3 KB
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
/**
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*/
var exist = function(board, word) {
const vis = new Array(board.length)
for (let i = 0; i < board.length; i++) {
vis[i] = new Array(board[0].length).fill(false)
}
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
vis[i][j] = true
if (board[i][j] === word[0] && dfs(board, word, i, j, vis)) {
return true
}
vis[i][j] = false
}
}
return false
};
function inside (board, x, y) {
return x >= 0 && y >= 0 && x < board.length && y < board[0].length
}
function dfs (board, word, x, y, vis) {
const dirs = [
[1, 0], [0, 1], [-1, 0], [0, -1]
]
function dfsHelper (x, y, index) {
if (index + 1 === word.length) {
return true
}
for (const d of dirs) {
let newx = x + d[0]
let newy = y + d[1]
if (inside(board, newx, newy) &&
board[newx][newy] === word[index + 1] &&
!vis[newx][newy]) {
vis[newx][newy] = true
if (dfsHelper(newx, newy, index + 1)) {
return true
}
vis[newx][newy] = false
}
}
return false
}
return dfsHelper(x, y, 0)
}
if (process.env.LZS) {
console.log(exist(
[['C', 'D', 'E', 'F'],
['E', 'R', 'T', 'F']]
, 'CDETFF'))
}