티스토리 뷰

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

 

21611번: 마법사 상어와 블리자드

마법사 상어는 파이어볼, 토네이도, 파이어스톰, 물복사버그, 비바라기 마법을 할 수 있다. 오늘 새로 배운 마법은 블리자드이고, 크기가 N×N인 격자에서 연습하려고 한다. N은 항상 홀수이고, (

www.acmicpc.net

 

const input = require('fs')
	.readFileSync('./dev/stdin')
	.toString()
	.trim()
	.split('\n')
	.map((v) => v.split(' ').map(Number));

const [N, M] = input.shift();
let board = [];
let boom = [0, 0, 0, 0];
for (let i = 0; i < N; i++) {
	board.push(input.shift());
}
const blizzard = input;

const curveArr = [];
let curvCnt = 1;
let total = 0;
while (total < N * N) {
	curveArr.push(curvCnt);
	curveArr.push(curvCnt);
	total += 2 * curvCnt;
	curvCnt++;
}
const dx = [0, -1, 0, 1];
const dy = [1, 0, -1, 0];

let indexBoard = Array.from(Array(N), () => Array(N).fill(0));
let x = (N - 1) / 2;
let y = (N - 1) / 2;
let dir = 2;
let num = 1;
let curve = curveArr.shift();
indexBoard[x][y] = 's';
let balls = [];
while (num < N * N) {
	x += dx[dir];
	y += dy[dir];
	indexBoard[x][y] = num - 1;

	balls.push(board[x][y]);
	curve--;
	if (curve == 0) {
		curve = curveArr.shift();
		dir = (dir + 1) % 4;
	}
	num++;
}
blizzard.forEach(([d, s]) => {
	const ox = (N - 1) / 2;
	const oy = (N - 1) / 2;
	const destroy = [];
	switch (d) {
		case 1:
			for (let i = ox - s; i < ox; i++) {
				destroy.push(indexBoard[i][oy]);
			}
			break;
		case 2:
			for (let i = ox + 1; i <= ox + s; i++) {
				destroy.push(indexBoard[i][oy]);
			}
			break;
		case 3:
			for (let i = oy - s; i < oy; i++) {
				destroy.push(indexBoard[ox][i]);
			}
			break;

		case 4:
			for (let i = oy + 1; i <= oy + s; i++) {
				destroy.push(indexBoard[ox][i]);
			}
			break;
	}
	balls = balls.filter((v, i) => !destroy.includes(i));
	//폭발
	/**
	 * Boom Boom Boom 내 심장이 뛰네
	 * Get it like Boom Boom Boom
	 * Get it like Boom Boom Boom
	 * Boom Boom now
	 */
	let boomboomboomGotMyHeartbeatPumping = true;
	while (boomboomboomGotMyHeartbeatPumping) {
		boomboomboomGotMyHeartbeatPumping = false;
		let newBalls = [];

		while (balls.length > 0) {
			const now = balls.shift();
			let cnt = 1;
			while (balls[0] == now) {
				balls.shift();
				cnt++;
			}
			if (cnt <= 3) {
				for (let c = 0; c < cnt; c++) {
					newBalls.push(now);
				}
			} else {
				boomboomboomGotMyHeartbeatPumping = true;
				boom[now] += cnt;
			}
		}
		balls = newBalls;
	}

	let newBalls = [];
	while (balls.length > 0 && newBalls.length < N * N - 1) {
		const now = balls.shift();
		let cnt = 1;
		while (balls[0] == now) {
			cnt++;
			balls.shift();
		}
		newBalls.push(cnt);
		newBalls.push(now);
	}
	balls = newBalls;
});

console.log(boom.reduce((r, v, i) => r + v * i, 0));
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
글 보관함