티스토리 뷰
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
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 2338번: 긴자리 계산 (0) | 2023.07.18 |
---|---|
Node.js) 백준 9328번: 열쇠 (0) | 2023.07.16 |
Node.js) 백준 18809번: Gaaaaaaaaaarden (0) | 2023.07.15 |
Node.js) 백준 1431번: 시리얼 번호 (0) | 2023.07.15 |
Node.js) 백준 11652번: 카드 (0) | 2023.07.15 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 그래프
- 동적프로그래밍
- BFS
- create db
- 서버점검
- 로드나인
- node.js
- 다이나밍프로그래밍
- 투포인터
- 서버개발
- 최소공통조상
- 은둔청년체험
- 롱베케이션
- 개발자면접
- MOD
- MySQL
- create databases;
- 다이나믹프로그래밍
- DB 생성
- 면접질문
- 면접비
- 투포인터 연습
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함