๐ŸŽฒ ๋ฐฑ์ค€ 16920๋ฒˆ ํ™•์žฅ๊ฒŒ์ž„

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

๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
130/186

๐ŸŽจ ํ ๊ตฌํ˜„ ์ฐธ๊ณ  ์ฝ”๋“œ
๐ŸŽจ ์ฐธ๊ณ  ์ฝ”๋“œ
๐Ÿ’Š ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•ด ํ๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.
๐Ÿ’Š player๋งˆ๋‹ค ํ๋ฅผ ๋”ฐ๋กœ ์„ค์ •ํ•ด์ค€๋‹ค.

const fs = require('fs'); 
let input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const [N, M, P] = input.shift().split(" ").map(Number);
const S = input.shift().split(" ").map(Number);
input = input.map((el) => el.split(""));

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

  isEmpty() {
    return this.front === null && this.rear === null;
  }

  insert(data) {
    const newNode = new Node(data);
    if (this.isEmpty()) this.front = newNode;
    else this.rear.next = newNode;

    this.rear = newNode;
    this.size++;
  }

  remove() {
    if (this.isEmpty()) return;
    this.front = this.front.next;
    if (!this.front) this.rear = null;
    this.size--;
  }

  peekFront() {
    return this.front.data;
  }

  length() {
    return this.size;
  }
}
const result = new Array(P).fill(0);
const dir = [
  [0, 1],
  [0, -1],
  [-1, 0],
  [1, 0],
];
const player = new Array(10);

const bfs = () => {
  //ํ”Œ๋ ˆ์ด์–ด ๊ฐ๊ฐ ํ๋ฅผ ๊ฐ–๊ธฐ๋•Œ๋ฌธ์— 2์ฐจ์›๋ฐฐ์—ด๋กœ ์„ค์ •
  const queue = Array.from(new Array(10), () => new Queue());
  for (let i = 0; i < N; i++) {
    for (let j = 0; j < M; j++) {
      if (input[i][j] !== "." && input[i][j] !== "#") {
        queue[input[i][j]].insert([i, j]);
        result[input[i][j] - 1]++;
      }
    }
  }

  const index = new Array(10).fill(0);
  while (true) {
    //๋ผ์šด๋“œ ์‹œ์ž‘
    let is_expand = false;
    //1๋ฒˆ ํ”Œ๋ ˆ์ด์–ด๋ถ€ํ„ฐ์‹œ์ž‘
    for (let i = 1; i <= P; i++) {
      let move_count = S[i - 1]; //i๋ฒˆ ํ”Œ๋ ˆ์ด์–ด์˜ ์ตœ๋Œ€ ์ด๋™ ํšŸ์ˆ˜ ์ €์žฅ
      while (queue[i].length() && move_count--) {
        const q_size = queue[i].length();
        for (let j = 0; j < q_size; j++) {
          const [pointY, pointX] = queue[i].peekFront();
          queue[i].remove()
          for (let d = 0; d < 4; d++) {
            const [nextY, nextX] = [pointY + dir[d][0], pointX + dir[d][1]];
            if (nextX < 0 || nextY < 0 || nextX >= M || nextY >= N) continue;
            if (input[nextY][nextX] === ".") {
              queue[i].insert([nextY, nextX]);
              input[nextY][nextX] = input[pointY][pointX];
              result[i - 1]++;
              is_expand = true;
            }
          }
        }
        index[i]++;
      }
    }
    if (!is_expand) break;
  }
};

bfs();
console.log(result.join(" "));

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