๐จ ํ ๊ตฌํ ์ฐธ๊ณ ์ฝ๋
๐จ ์ฐธ๊ณ ์ฝ๋
๐ ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํด ํ๋ฅผ ์ง์ ๊ตฌํํ์๋ค.
๐ 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(" "));