-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path79-word-search.js
119 lines (116 loc) · 3.16 KB
/
79-word-search.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*/
// var exist = function (board, word) {
// const direction = [
// [1, 0],
// [0, 1],
// [-1, 0],
// [0, -1],
// ];
// const isValid = (x, y) => {
// return x >= 0 && y >= 0 && x < board[0].length && y < board.length;
// };
// for (let i = 0; i < board[0].length; i++) {
// for (let j = 0; j < board.length; j++) {
// const queue = [[i, j, 0, new Set()]];
// while (queue.length > 0) {
// const [x, y, charIndex, visited] = queue.shift();
// if (board[y][x] !== word[charIndex]) {
// continue;
// }
// if (charIndex === word.length - 1) {
// return true;
// }
// visited.add(`${x}-${y}`);
// for ([dx, dy] of direction) {
// if (isValid(x + dx, y + dy) && !visited.has(`${x + dx}-${y + dy}`)) {
// queue.push([x + dx, y + dy, charIndex + 1, new Set(visited)]);
// }
// }
// }
// }
// }
// return false;
// };
// var exist = function (board, word) {
// const search = (x, y, charIndex, visited) => {
// if (board[y][x] !== word[charIndex]) {
// return false;
// }
// if (charIndex === word.length - 1) {
// return true;
// }
// charIndex++;
// visited[y][x] = true;
// let result = false;
// if (x - 1 >= 0 && !visited[y][x - 1]) {
// result = result || search(x - 1, y, charIndex, visited);
// }
// if (x + 1 < board[0].length && !visited[y][x + 1]) {
// result = result || search(x + 1, y, charIndex, visited);
// }
// if (y - 1 >= 0 && !visited[y - 1][x]) {
// result = result || search(x, y - 1, charIndex, visited);
// }
// if (y + 1 < board.length && !visited[y + 1][x]) {
// result = result || search(x, y + 1, charIndex, visited);
// }
// visited[y][x] = false;
// return result;
// };
// for (let i = 0; i < board[0].length; i++) {
// for (let j = 0; j < board.length; j++) {
// const visited = new Array(board.length).fill().map(() => new Array(board[0].length).fill(false));
// if (search(i, j, 0, visited)) {
// return true;
// }
// }
// }
// return false;
// };
var exist = function (board, word) {
const search = (x, y, charIndex) => {
if (board[y][x] !== word[charIndex] || board[y][x] === 1) {
return false;
}
if (charIndex === word.length - 1) {
return true;
}
const original = board[y][x];
board[y][x] = 1;
const direction = [
[1, 0],
[0, 1],
[-1, 0],
[0, -1],
];
for ([dx, dy] of direction) {
if (x + dx >= 0 && y + dy >= 0 && x + dx < board[0].length && y + dy < board.length && search(x + dx, y + dy, charIndex + 1)) {
return true;
}
}
board[y][x] = original;
return false;
};
for (let i = 0; i < board[0].length; i++) {
for (let j = 0; j < board.length; j++) {
if (search(i, j, 0)) {
return true;
}
}
}
return false;
};
console.error(
exist(
[
["A", "B", "C", "E"],
["S", "F", "C", "S"],
["A", "D", "E", "E"],
],
"ABCB"
)
);