-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.js
More file actions
40 lines (34 loc) · 769 Bytes
/
Copy pathdfs.js
File metadata and controls
40 lines (34 loc) · 769 Bytes
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
function ice(input) {
const splitedInput = input.split("\n");
const [n, m] = splitedInput[0].split(" ");
const graph = splitedInput.slice(1);
let result = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
if (dfs(i, j)) {
result += 1;
}
}
}
function dfs(x, y) {
if (x <= -1 || x >= n || y <= -1 || y >= m) {
return false;
}
if (graph[x][y] == 0) {
graph[x][y] = 1;
dfs(x - 1, y);
dfs(x, y - 1);
dfs(x + 1, y);
dfs(x, y + 1);
return true;
}
return false;
}
console.log(result);
return result;
}
ice(`4 5
00110
00011
11111
00000`);