-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathflood-fill.js
More file actions
74 lines (64 loc) · 1.63 KB
/
flood-fill.js
File metadata and controls
74 lines (64 loc) · 1.63 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import Queue from '../../datastructure/FakeQueue_immutable'
import sort2DArr from './flood-fill'
function neighbors(p) {
let top = [p[0]-1, p[1]]
let bottom = [p[0]+1, p[1]]
let left = [p[0], p[1]-1]
let right = [p[0], p[1]+1]
return {top, bottom, left, right}
}
function isSameColor(grid, p1, p2) {
// also ensures the points are valid locations within the grid
if(isNotInGrid(grid, p1) || isNotInGrid(grid, p2)) {
return false
}
return grid[p1[0]][p1[1]] === grid[p2[0]][p2[1]]
}
function isNotInGrid(grid, p) {
if(typeof p === 'undefined') return true
if (typeof grid[p[0]] === 'undefined') return true
return false
}
function findMaxConnectedDFS(grid, p) {
let explored = {}
explored[p] = true
let res = [p]
// explore neighbors via DFS using recursion
function explore(p) {
Object.values(neighbors(p)).forEach(neighbor => {
if(isSameColor(grid, p, neighbor) && !explored[neighbor]) {
res.push(neighbor)
explored[neighbor] = true
return explore(neighbor)
}
})
}
explore(p,res, explored)
return sort2DArr(res)
}
function findMaxConnectedBFS(grid, p) {
let explored = {}
let u = p
let res = [u]
let q = new Queue()
q = q.enqueue(u)
explored[u] = true
// explore neighbors via BFS using a queue and a while-loop
while(!q.isEmpty()) {
[u, q] = q.dequeue()
Object.values(neighbors(u)).forEach(v => {
if (!explored[v]) {
explored[v] = true
if(isSameColor(grid, u, v)) {
res.push(v)
q = q.enqueue(v)
}
}
})
}
return sort2DArr(res)
}
export {
findMaxConnectedDFS,
findMaxConnectedBFS
}