๐จ ์ฐธ๊ณ
๐ ๋ค์ ๋ฐฉ์ ๊ฐ๋ ๋ถ์ด ์์ผ์ ธ์๋ ๊ฒฝ์ฐ candidates์ ์ถ๊ฐํ๊ณ ๋ถ์ ํค๋ ๊ณผ์ ์์ candidates์ ์์ผ๋ฉด queue์ ๋ฃ์ด์ฃผ๋ ๊ฒ์ด ํต์ฌ์ด๋ค.
์ฝ๋
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const [N, M] = input.shift().split(" ").map(Number);
const switchArr = Array.from(new Array(N + 1), () =>
Array.from(new Array(N + 1), () => [])
);
//0:๋ถ๊บผ์ง 1:๋ถ์ผ์ง 2:๋ฐฉ๋ฌธํจ
const lightArr = Array.from(new Array(N + 1), () => new Array(N + 1).fill(0));
const candidates = new Set();
for (let i = 0; i < M; i++) {
const [x, y, a, b] = input[i].split(" ").map(Number);
switchArr[y][x].push([a, b]);
}
const dir = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];
let answer = 1;
const queue = [[1, 1]];
lightArr[1][1] = 2;
const bfs = () => {
while (queue.length) {
const [x, y] = queue.shift();
answer;
for (let i = 0; i < switchArr[y][x].length; i++) {
const [onX, onY] = switchArr[y][x][i];
if (lightArr[onY][onX] === 0) {
lightArr[onY][onX] = 1;
answer++;
if (candidates.has(JSON.stringify([onX, onY]))) {
queue.push([onX, onY]);
}
}
}
for (let d = 0; d < 4; d++) {
const [nextX, nextY] = [x + dir[d][0], y + dir[d][1]];
if (
nextX < 1 ||
nextY < 1 ||
nextX > N ||
nextY > N ||
lightArr[nextY][nextX] === 2
)
continue;
if (lightArr[nextY][nextX] === 0) {
candidates.add(JSON.stringify([nextX, nextY]));
} else {
lightArr[nextY][nextX] = 2;
queue.push([nextX, nextY]);
}
}
}
};
bfs();
console.log(answer);