๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿง  ์•Œ๊ณ ๋ฆฌ์ฆ˜

[๋ฐฑ์ค€] 2573 ๋น™์‚ฐ js ํ’€์ด

by megan07 2023. 12. 28.

๐Ÿ’ก ์ ‘๊ทผ๋ฒ•

  1. ํ–‡์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค
    1-1. ๋งค๋…„ ๋น™์‚ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค iceCnt
    1-2. ๋น™์‚ฐ์˜ ๋†’์ด๋ฅผ ๋ฐ”๊ฟ”์ค„ ๋•Œ๋งˆ๋‹ค ๋น™์‚ฐ์˜ ๊ฐœ์ˆ˜ -1 ํ•ด์ฃผ๊ณ (iceCnt--)
    1-3. ๋ฐ”๊ฟ”์•ผ ํ•  ๋น™์‚ฐ์ด ๋‚จ์ง€ ์•Š์•˜์„ ๋•Œ, ์ƒˆ๋กœ์šด ํ•ด๊ฐ€ ์‹œ์ž‘๋˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค(year++)
  2. ๋ชจ๋“  ๋น™์‚ฐ์ด ๋…น์„ ๋•Œ๊นŒ์ง€ ๋น™์‚ฐ์˜ ๋†’์ด๋ฅผ ์กฐ์ ˆํ•œ๋‹ค.(bfs์‚ฌ์šฉ)
    2-1. ๋น™์‚ฐ์˜ ๋†’์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด์€ 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
    (why? ๋ฐฐ์—ด์„ 1๊ฐœ๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ์—…๋ฐ์ดํŠธ ํ•ด์ค„ ๊ฒฝ์šฐ, ๊ฐ™์€ ํ•ด์— ๋…น์•„์„œ 0์ด ๋œ ๋น™์‚ฐ์ด ์ธ์ ‘ ๋น™์‚ฐ์˜ ๋†’์ด๋ฅผ ์กฐ์ ˆํ•  ๋•Œ ๋ฐ”๋‹ค๋กœ ์นด์šดํŠธ ๋˜๊ธฐ ๋•Œ๋ฌธ์—)
    2-2. ์˜ค๋ฆฌ์ง€๋„ ๋ฐฐ์—ด(=์ƒˆํ•ด๊ฐ€ ์‹œ์ž‘๋˜๊ณ  ์•„์ง ๋น™์‚ฐ์ด ๋…น์ง€ ์•Š์€ ๋ฐฐ์—ด)๊ธฐ์ค€์œผ๋กœ ์ธ์ ‘ ๋ฐ”๋‹ค๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์ž„์‹œ ๋ฐฐ์—ด์— ๋ณ€ํ•œ ๋น™์‚ฐ์˜ ๋†’์ด๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค(bfs์‚ฌ์šฉ)
    2-3. ๋†’์ด๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฉด ๋‹ค์‹œ ํ์— ๋„ฃ์–ด์ค€๋‹ค
    2-4. ๋ฐ”๊ฟ”์•ผ ํ•  ๋น™์‚ฐ์ด ๋‚จ์ง€ ์•Š์•˜์„ ๋•Œ, ์˜ค๋ฆฌ์ง€๋„ ๋ฐฐ์—ด์— ์ž„์‹œ ๋ฐฐ์—ด์˜ ๊ฐ’์€ ๋ณต์‚ฌํ•ด์ค€๋‹ค
  3. ๋น™์‚ฐ์ด 2๊ฐœ๋กœ ๋‚˜๋‰˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. (dfs ์‚ฌ์šฉ)
    3-1. ๋น™์‚ฐ์˜ ๋†’์ด๋ฅผ ๋ชจ๋‘ ๋ฐ”๊พธ๊ณ  ์‹œ๊ฐ„(๋…„)์„ +1 ํ•ด์ฃผ๊ธฐ ์ „์— ๋น™์‚ฐ์ด 2 ๋ฉ์–ด๋ฆฌ๋กœ ๋‚˜๋‰˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
    3-2. checkํ•จ์ˆ˜์˜ ์ธ์ž๋กœ๋Š” ํ์— ๋“ค์–ด๊ฐ€์žˆ๋Š” ์ฒซ ๋ฒˆ์งธ ๋น™์‚ฐ์˜ [row index,column index]์™€ ๋‚จ์•„์žˆ๋Š” ๋น™์‚ฐ(ํ์— ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๋น™์‚ฐ)์˜ ๊ฐœ์ˆ˜๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.
    3-3. dfs๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•ด๋‹น ์›์†Œ์—์„œ๋ถ€ํ„ฐ ํ•œ ๋ฉ์–ด๋ฆฌ๋ฅผ ์ด๋ฃจ๋Š” ์›์†Œ๋“ค์˜ ๊ฐœ์ˆ˜(cnt)๋ฅผ ๊ตฌํ•œ๋‹ค
    3-4. ํ์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’๊ณผ cnt์˜ ๊ฐ’์ด ๊ฐ™์ง€ ์•Š์œผ๋ฉด 2๋ฉ์–ด๋ฆฌ๋กœ ๋ถ„๋ฆฌ๋œ ๊ฒƒ์ž„์œผ๋กœ year๋ฅผ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

 

 

ํ๋Š” ๊ฐ„๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ญ์ƒ ์ง์ ‘ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค

class Node {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}

class Queue {
  constructor() {
    this.front = null;
    this.rear = null;
    this.size = 0;
  }

  enqueue(val) {
    const node = new Node(val);
    if (!this.front) {
      this.front = node;
      this.rear = node;
    } else {
      this.rear.next = node;
      this.rear = node;
    }

    return ++this.size;
  }

  dequeue() {
    if (!this.front) return null;

    const tmp = this.front;
    if (this.front === this.rear) {
      this.rear = null;
    }

    this.front = this.front.next;
    this.size--;
    return tmp.val;
  }
}


const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';

// ์ž…๋ ฅ๊ฐ’์ด ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ž…๋ ฅ ๊ฐ’์˜ ๊ธธ์ด(n), n๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ํ•œ ์ค„์— ํ•˜๋‚˜์˜ ์ž…๋ ฅ๊ฐ’์ด ์ฃผ์–ด์งˆ ๋•Œ
const [n, ...input] = require('fs')
  .readFileSync(filePath)
  .toString()
  .trim()
  .split('\n');

/**
 * ์ ‘๊ทผ๋ฒ•
 * 1. ํ–‡์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค
 *  1-1. ๋งค๋…„ ๋น™์‚ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค iceCnt
 *  1-2. ๋น™์‚ฐ์˜ ๋†’์ด๋ฅผ ๋ฐ”๊ฟ”์ค„ ๋•Œ๋งˆ๋‹ค ๋น™์‚ฐ์˜ ๊ฐœ์ˆ˜ -1 ํ•ด์ฃผ๊ณ (iceCnt--)
 *  1-3. ๋ฐ”๊ฟ”์•ผ ํ•  ๋น™์‚ฐ์ด ๋‚จ์ง€ ์•Š์•˜์„ ๋•Œ, ์ƒˆ๋กœ์šด ํ•ด๊ฐ€ ์‹œ์ž‘๋˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค(year++)
 *
 * 2. ๋ชจ๋“  ๋น™์‚ฐ์ด ๋…น์„ ๋•Œ๊นŒ์ง€ ๋น™์‚ฐ์˜ ๋†’์ด๋ฅผ ์กฐ์ ˆํ•œ๋‹ค.(bfs์‚ฌ์šฉ)
 *  2-1. ๋น™์‚ฐ์˜ ๋†’์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด์€ 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค
 *    *  ๋ฐฐ์—ด์„ 1๊ฐœ๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ์—…๋ฐ์ดํŠธ ํ•ด์ค„ ๊ฒฝ์šฐ, ๊ฐ™์€ ํ•ด์— ๋…น์•„์„œ 0์ด ๋œ ๋น™์‚ฐ์ด ์ธ์ ‘ ๋น™์‚ฐ์˜ ๋†’์ด๋ฅผ ์กฐ์ ˆํ•  ๋•Œ ๋ฐ”๋‹ค๋กœ ์นด์šดํŠธ ๋˜๊ธฐ ๋•Œ๋ฌธ์—
 *  2-2. ์˜ค๋ฆฌ์ง€๋„ ๋ฐฐ์—ด(=์ƒˆํ•ด๊ฐ€ ์‹œ์ž‘๋˜๊ณ  ์•„์ง ๋น™์‚ฐ์ด ๋…น์ง€ ์•Š์€ ๋ฐฐ์—ด)๊ธฐ์ค€์œผ๋กœ ์ธ์ ‘ ๋ฐ”๋‹ค๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์ž„์‹œ ๋ฐฐ์—ด์— ๋ณ€ํ•œ ๋น™์‚ฐ์˜ ๋†’์ด๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค(bfs์‚ฌ์šฉ)
 *  2-3. ๋†’์ด๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฉด ๋‹ค์‹œ ํ์— ๋„ฃ์–ด์ค€๋‹ค
 *  2-4. ๋ฐ”๊ฟ”์•ผ ํ•  ๋น™์‚ฐ์ด ๋‚จ์ง€ ์•Š์•˜์„ ๋•Œ, ์˜ค๋ฆฌ์ง€๋„ ๋ฐฐ์—ด์— ์ž„์‹œ ๋ฐฐ์—ด์˜ ๊ฐ’์€ ๋ณต์‚ฌํ•ด์ค€๋‹ค
 *
 * 3. ๋น™์‚ฐ์ด 2๊ฐœ๋กœ ๋‚˜๋‰˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
 *  3-1. ๋น™์‚ฐ์˜ ๋†’์ด๋ฅผ ๋ชจ๋‘ ๋ฐ”๊พธ๊ณ  ์‹œ๊ฐ„(๋…„)์„ +1 ํ•ด์ฃผ๊ธฐ ์ „์— ๋น™์‚ฐ์ด 2 ๋ฉ์–ด๋ฆฌ๋กœ ๋‚˜๋‰˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
 *  3-2. checkํ•จ์ˆ˜์˜ ์ธ์ž๋กœ๋Š” ํ์— ๋“ค์–ด๊ฐ€์žˆ๋Š” ์ฒซ ๋ฒˆ์งธ ๋น™์‚ฐ์˜ [row index,column index]์™€ ๋‚จ์•„์žˆ๋Š” ๋น™์‚ฐ(ํ์— ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๋น™์‚ฐ)์˜ ๊ฐœ์ˆ˜๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.
 *  3-3. dfs๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•ด๋‹น ์›์†Œ์—์„œ๋ถ€ํ„ฐ ํ•œ ๋ฉ์–ด๋ฆฌ๋ฅผ ์ด๋ฃจ๋Š” ์›์†Œ๋“ค์˜ ๊ฐœ์ˆ˜(cnt)๋ฅผ ๊ตฌํ•œ๋‹ค
 *  3-4. ํ์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’๊ณผ cnt์˜ ๊ฐ’์ด ๊ฐ™์ง€ ์•Š์œผ๋ฉด 2๋ฉ์–ด๋ฆฌ๋กœ ๋ถ„๋ฆฌ๋œ ๊ฒƒ์ž„์œผ๋กœ year๋ฅผ ๋ฆฌํ„ดํ•ด์ค€๋‹ค
 */

function solution(N, M, Map) {
  const ice = new Queue();

  //๋น™์‚ฐ์˜ ๊ฐœ์ˆ˜
  let iceCnt;

  //์‹œ๊ฐ„
  let year = 1;

  const dr = [1, -1, 0, 0];
  const dc = [0, 0, 1, -1];

  //์ž„์‹œ ๋ฐฐ์—ด
  const tmpMap = [...Map.map((r) => [...r])];

  //์ง€๋„๋ฅผ ๋Œ๋ฉด์„œ ๋น™์‚ฐ์ด ์žˆ๋Š” ๋ถ€๋ถ„์˜ ์ธ๋ฑ์Šค๋ฅผ ํ์— ๋„ฃ์–ด์ค€๋‹ค
  Map.forEach((r, rIdx) => {
    r.forEach((c, cIdx) => {
      if (c !== 0) {
        ice.enqueue([rIdx, cIdx]);
      }
    });
  });

  //๋น™ํ•˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค
  iceCnt = ice.size;

  //๋น™ํ•˜๊ฐ€ ๋‹ค ๋…น์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค
  while (ice.size) {
    //๋ชจ๋“  ๋น™ํ•˜์˜ ๋†’์ด๋ฅผ ์กฐ์ ˆํ–ˆ์„ ๋•Œ
    if (iceCnt === 0) {
      //์˜ค๋ฆฌ์ง€๋„ ๋ฐฐ์—ด์— ์ž„์‹œ๋ฐฐ์—ด์˜ ๊ฐ’์„ ๋ณต์‚ฌํ•ด์ค€๋‹ค
      Map = [...tmpMap.map((r) => [...r])];

      //2๋ฉ์–ด๋ฆฌ๋กœ ๋‚˜๋‰˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค
      const [r, c] = ice.front.val;
      if (check(r, c, ice.size)) return year;

      year++;
      iceCnt = ice.size;
    }

    const [r, c] = ice.dequeue();

    //์ธ์ ‘ ๋ฐ”๋‹ค ๊ฐœ์ˆ˜
    let sea = 0;

    for (let i = 0; i < 4; i++) {
      const nextR = r + dr[i];
      const nextC = c + dc[i];
      if (
        nextR >= 0 &&
        nextR < N &&
        nextC >= 0 &&
        nextC < M &&
        !Map[nextR][nextC]
      ) {
        sea++;
      }
    }

    //์กฐ์ ˆํ•œ ๋น™์‚ฐ์˜ ๋†’์ด๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฉด ํ์— ๋‹ค์‹œ ๋„ฃ์–ด์ค€๋‹ค
    if (Map[r][c] - sea > 0) {
      ice.enqueue([r, c]);
      tmpMap[r][c] -= sea;
    } else {
      tmpMap[r][c] = 0;
    }

    //๋†’์ด ์กฐ์ ˆํ•ด์•ผ ํ•˜๋Š” ๋น™์‚ฐ์˜ ๊ฐœ์ˆ˜ -1
    iceCnt--;
  }

  function check(r, c, size) {
    const visited = Array.from(new Array(N), () => new Array(M).fill(false));
    let cnt = 0;
    cnt++;
    visited[r][c] = true;
    dfs(r, c);

    function dfs(r, c) {
      for (let i = 0; i < 4; i++) {
        const nextR = r + dr[i];
        const nextC = c + dc[i];
        if (
          nextR >= 0 &&
          nextR < N &&
          nextC >= 0 &&
          nextC < M &&
          Map[nextR][nextC] &&
          !visited[nextR][nextC]
        ) {
          cnt++;
          visited[nextR][nextC] = true;
          dfs(nextR, nextC);
        }
      }
    }

    return cnt !== size;
  }

  return 0;
}

const arr = n.split(' ').map(Number);

const answer = solution(
  arr[0],
  arr[1],
  input.map((r) => r.split(' ').map(Number)),
);

console.log(answer);