-
Notifications
You must be signed in to change notification settings - Fork 3
/
connectedCell.js
56 lines (54 loc) · 1.35 KB
/
connectedCell.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
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
function connectedCell(matrix) {
const h = matrix.length,
w = matrix[0].length;
let max = 0;
const find = (x, y) => {
let count = 1;
matrix[y][x] = 0;
for (const stack = [
[x, y]
]; stack.length && ([x, y] = stack.pop());) {
// Check the neighbors recursively
for (let i = -1; ++i < 9;) {
const xx = x + Math.floor(i / 3) - 1,
yy = y + i % 3 - 1;
// Skip center, out of bounds and not filled
if (yy > -1 && yy < h && xx > -1 && xx < w && matrix[yy][xx]) {
++count;
matrix[yy][xx] = 0;
stack.push([xx, yy]);
}
}
}
return count;
};
for (let y = h; y--;)
for (let x = w; x--;)
if (matrix[y][x])
max = Math.max(max, find(x, y));
return max;
}
function recursiveConnectedCell(matrix) {
const h = matrix.length,
w = matrix[0].length;
let max = 0;
const find = (x, y) => {
// Return 0 if it's out of bounds or not filled
if (y < 0 || y >= h || x < 0 || x >= w || !matrix[y][x])
return 0;
// Mark as "visited", so it won't be checked again
matrix[y][x] = 0;
let count = 1;
// Check the neighbors recursively
for (let i = -1; ++i < 9;)
count += find(x + Math.floor(i / 3) - 1, y + i % 3 - 1);
return count;
};
for (let y = h; y--;)
for (let x = w; x--;)
if (matrix[y][x])
max = Math.max(max, find(x, y));
return max;
}