티스토리 뷰

https://www.acmicpc.net/problem/16920

 

16920번: 확장 게임

구사과와 친구들이 확장 게임을 하려고 한다. 이 게임은 크기가 N×M인 격자판 위에서 진행되며, 각 칸은 비어있거나 막혀있다. 각 플레이어는 하나 이상의 성을 가지고 있고, 이 성도 격자판 위

www.acmicpc.net

 

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

class Queue {
	constructor() {
		this.head = null;
		this.tail = null;
		this.length = 0;
	}

	push(item) {
		const node = new Node(item);
		if (this.head == null) {
			this.head = node;
		} else {
			this.tail.next = node;
		}

		this.tail = node;
		this.length += 1;
	}

	pop() {
		const popItem = this.head;
		this.head = this.head.next;
		this.length -= 1;
		return popItem.item;
	}
}
const fs = require('fs');
let input = fs
	.readFileSync('./dev/stdin')
	.toString()
	.trim()
	.split('\n')
	.map((v) => v.trim());
const [N, M, P] = input.shift().split(' ').map(Number);
const S = input.shift().split(' ').map(Number);
S.unshift(0);
let board = input.map((v) => v.split(''));
const q = Array.from(Array(P + 1), () => new Queue());
let score = Array(P + 1).fill(0);

for (let i = 0; i < N; i++) {
	for (let j = 0; j < M; j++) {
		const num = Number(board[i][j]);
		if (!Number.isNaN(num)) {
			q[num].push([i, j]);
			score[num]++;
		}
	}
}

const dx = [0, 0, -1, 1];
const dy = [-1, 1, 0, 0];

let CONTINUE = true;
while (CONTINUE) {
	CONTINUE = false;
	for (let p = 1; p <= P; p++) {
		for (let move = S[p]; move > 0; move--) {
			const L = q[p].length;
			if (L == 0) break;
			for (let l = 0; l < L; l++) {
				const [x, y] = q[p].pop();
				for (let k = 0; k < 4; k++) {
					const nx = x + dx[k];
					const ny = y + dy[k];
					if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
					if (board[nx][ny] == '.') {
						q[p].push([nx, ny]);
						board[nx][ny] = p;
						score[p]++;
						CONTINUE = true;
					}
				}
			}
		}
	}
}
console.log(score.slice(1).join(' '));
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함