-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavascript.js
57 lines (57 loc) · 1.42 KB
/
Javascript.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
const N = parseInt(readline());
let L = parseInt(readline());
let arr = [];
for (let i = 0; i < N; i++) {
arr.push(readline().split(" "));
}
for (let i = 0; i < arr.length; i++) {
//
for (let j = 0; j < arr.length; j++) {
//
if (arr[i][j] == "C") {
arr[i][j] = L;
} // change the values of the array if C put the value of L otherwise 0
else {
arr[i][j] = 0;
} //
} //
}
let indexChecker = [
{ i: 1, j: 0 },
{ i: 1, j: -1 },
{ i: 1, j: 1 },
{ i: -1, j: 0 },
{ i: -1, j: -1 },
{ i: -1, j: 1 },
{ i: 0, j: -1 },
{ i: 0, j: 1 },
]; //index to check
while (L != 0) {
//
for (let i = 0; i < arr.length; i++) {
// loop that fills the table with
for (let j = 0; j < arr.length; j++) {
// values based on the light
if (arr[i][j] == L) {
// emitted by a candle.
indexChecker.forEach((e) => {
//
if (i + e.i >= 0 && i + e.i < arr.length && j + e.j >= 0 && j + e.j < arr.length && arr[i + e.i][j + e.j] < L) {
// uses the "indexChecker" list
arr[i + e.i][j + e.j] = L - 1; // to scan the neighbors
} //
}); // If the cell value is lower
} // then replace it with L-1
} //
} // at the end of the loop
L--; // removed 1 from L
} //
let count = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (arr[i][j] == 0) {
count++;
}
}
}
console.log(count);