๐ŸŽฒ ๋ฐฑ์ค€ 11967๋ฒˆ ๋ถˆ์ผœ๊ธฐ

Jeongeunยท2023๋…„ 11์›” 12์ผ
0

๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
133/187

๋ฐฑ์ค€ 11967๋ฒˆ

๐ŸŽจ ์ฐธ๊ณ 
๐Ÿ’Š ๋‹ค์Œ ๋ฐฉ์„ ๊ฐˆ๋•Œ ๋ถˆ์ด ์•ˆ์ผœ์ ธ์žˆ๋Š” ๊ฒฝ์šฐ 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);

0๊ฐœ์˜ ๋Œ“๊ธ€