-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2589.js
56 lines (51 loc) · 1.08 KB
/
2589.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
// Title: 보물섬
// Time: O(nm^2)
// Space: O(nm^2)
let input = require("fs")
.readFileSync(__dirname + "/input.txt")
.toString()
.trim()
.split("\n");
const [n, m] = input.shift().split(" ").map(Number);
input = input.map((i) => i.split(""));
let result = 0;
const direction = [
[-1, 0],
[0, 1],
[1, 0],
[0, -1],
];
for (let x = 0; x < n; x++) {
for (let y = 0; y < m; y++) {
if (input[x][y] === "L") {
bfs(x, y);
}
}
}
console.log(result);
function bfs(x, y) {
const ch = Array.from({ length: n }, () => Array(m).fill(0));
ch[x][y] = 1;
const queue = [[x, y, 0]];
let index = 0;
while (queue.length > index) {
const [x, y, dis] = queue[index++];
for (let i = 0; i < 4; i++) {
const [dx, dy] = direction[i];
const nx = x + dx;
const ny = y + dy;
if (
0 <= nx &&
nx < n &&
0 <= ny &&
ny < m &&
input[nx][ny] === "L" &&
ch[nx][ny] === 0
) {
if (result < dis + 1) result = dis + 1;
ch[nx][ny] = 1;
queue.push([nx, ny, dis + 1]);
}
}
}
}