-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
132 lines (124 loc) · 3.3 KB
/
util.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
120
121
122
123
124
125
126
127
128
129
130
131
132
class Matrix {
constructor(n, m) {
this.numrows = n;
this.numcolumns = m;
this.generateMatrix();
}
generateMatrix() {
var rows = [];
for (var j = 0; j < this.numrows; j++) {
var columns = [];
for (var i = 0; i < this.numcolumns; i++) {
columns.push({
val: Math.round(Math.random()),
visited: false
});
}
rows.push(columns);
}
this.data = rows;
}
seed(data) {
var self = this;
data.forEach(function(rows, RowIndex) {
rows.forEach(function(data, ColIndex) {
self.data[RowIndex][ColIndex].val = data;
self.data[RowIndex][ColIndex].visited = false;
});
});
}
print() {
for (var j = 0; j < this.data.length; j++) {
console.log(
this.data[j].map(function(V) {
return V.val;
})
);
}
}
iterate(callback) {
this.data.forEach(function(V, i) {
V.forEach(function(U, j) {
callback(U, i, j);
});
});
}
findConnectedNeighbour(i, j, collection) {
// since we are visiting, i,j, lets put visited true
this.getData(i, j).visited = true;
collection.push(`${i}x${j}`);
//Left
var canWeGoLeft =
j - 1 >= 0 &&
this.getData(i, j - 1).visited === false &&
this.getData(i, j - 1).val === 1;
if (canWeGoLeft) {
this.findConnectedNeighbour(i, j - 1, collection);
}
//Right
var canWeGoRight =
j + 1 <= this.numcolumns - 1 &&
this.getData(i, j + 1).visited === false &&
this.getData(i, j + 1).val === 1;
if (canWeGoRight) {
this.findConnectedNeighbour(i, j + 1, collection);
}
//UP
var canWeGoUp =
i - 1 >= 0 &&
this.getData(i - 1, j).visited === false &&
this.getData(i - 1, j).val === 1;
if (canWeGoUp) {
this.findConnectedNeighbour(i - 1, j, collection);
}
//Down
var canWeGoDown =
i + 1 <= this.numrows - 1 &&
this.getData(i + 1, j).visited === false &&
this.getData(i + 1, j).val === 1;
if (canWeGoDown) {
this.findConnectedNeighbour(i + 1, j, collection);
}
}
solve() {
// Return the total iselands of connected 1.
//Step 1 - iterate over all the elements and put flag - visited is true, so that, we will not process it again.
// If you find, 1, then Put inside island and Keep on expanding island, until it is not possible to extend.
// then resume the process.
//console.log("Solving Matrix");
var self = this;
var allIsLands = [];
this.iterate(function(cell, i, j) {
//Value, Row index, Col Index
//console.log("visiting cell", cell);
if (cell.visited === false) {
cell.visited = true;
if (cell.val === 1) {
//found island;
var island = [];
//console.log("found starting cell");
self.findConnectedNeighbour(i, j, island);
//console.log(`We have found island with length ${island.length}`,island);
allIsLands.push(island);
}
}
});
return allIsLands;
}
getData(i, j) {
//index start with 0;
try {
return this.data[i][j];
} catch (error) {
console.log("Error accessing data", i, j);
}
}
printLarge() {
this.iterate(function(U, i, j) {
console.log(U, i, j);
});
}
}
module.exports = {
Matrix
};