티스토리 뷰

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

 

23289번: 온풍기 안녕!

유난히 추운 날씨가 예상되는 이번 겨울을 대비하기 위해 구사과는 온풍기를 설치하려고 한다. 온풍기의 성능을 테스트하기 위해 구사과는 집을 크기가 R×C인 격자판으로 나타냈고, 1×1 크기

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 input = require('fs')
	.readFileSync('./dev/stdin')
	.toString()
	.trim()
	.split('\n')
	.map((v) => v.split(' ').map(Number));

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

const dx = [0, 0, -1, 1];
const dy = [1, -1, 0, 0];
const dw = [checkRightWall, checkLeftWall, checkTopWall, checkBottomWall];

/**
 * 오 1
 * 왼 2
 * 위 4
 * 아래 8
 */

function checkRightWall(x, y) {
	if ((wall[x][y] & (1 << 0)) > 0) return true;
	return false;
}

function checkLeftWall(x, y) {
	if ((wall[x][y] & (1 << 1)) > 0) return true;
	return false;
}

function checkTopWall(x, y) {
	if ((wall[x][y] & (1 << 2)) > 0) return true;
	return false;
}

function checkBottomWall(x, y) {
	if ((wall[x][y] & (1 << 3)) > 0) return true;
	return false;
}

const d = [
	[
		[
			-1,
			1,
			(x, y, nx, ny) => {
				if (checkTopWall(x, y)) return true;
				if (checkLeftWall(nx, ny)) return true;
				return false;
			},
		], // src의  위,   dest의  왼쪽
		[
			0,
			1,
			(x, y, nx, ny) => {
				if (checkRightWall(x, y)) return true;
				return false;
			},
		], // src의  오른쪽
		[
			1,
			1,
			(x, y, nx, ny) => {
				if (checkBottomWall(x, y)) return true;
				if (checkLeftWall(nx, ny)) return true;
				return false;
			},
		], // src의  아래, dest의 왼쪽
	], // 오
	[
		[
			-1,
			-1,
			(x, y, nx, ny) => {
				if (checkTopWall(x, y)) return true;
				if (checkRightWall(nx, ny)) return true;
				return false;
			},
		], // src의  위. dest 의 오른쪽
		[
			0,
			-1,
			(x, y, nx, ny) => {
				if (checkLeftWall(x, y)) return true;
				return false;
			},
		], // src의  왼쪽
		[
			1,
			-1,
			(x, y, nx, ny) => {
				if (checkBottomWall(x, y)) return true;
				if (checkRightWall(nx, ny)) return true;
				return false;
			},
		], // src의  아래, dest의 오른쪽
	], //왼
	[
		[
			-1,
			-1,
			(x, y, nx, ny) => {
				if (checkLeftWall(x, y)) return true;
				if (checkBottomWall(nx, ny)) return true;
				return false;
			},
		], // src의  왼쪽, dest의 아래
		[
			-1,
			0,
			(x, y, nx, ny) => {
				if (checkTopWall(x, y)) return true;
				return false;
			},
		], // src의  위
		[
			-1,
			1,
			(x, y, nx, ny) => {
				if (checkRightWall(x, y)) return true;
				if (checkBottomWall(nx, ny)) return true;
				return false;
			},
		], //  src의  오른쪽, dest의 아래
	], // 위
	[
		[
			1,
			-1,
			(x, y, nx, ny) => {
				if (checkLeftWall(x, y)) return true;
				if (checkTopWall(nx, ny)) return true;
				return false;
			},
		], // src의  왼쪽, dest의 위
		[
			1,
			0,
			(x, y, nx, ny) => {
				if (checkBottomWall(x, y)) return true;
				return false;
			},
		], //  src의  아래,
		[
			1,
			1,
			(x, y, nx, ny) => {
				if (checkRightWall(x, y)) return true;
				if (checkTopWall(nx, ny)) return true;
				return false;
			},
		], //  src의  오른쪽 dest의 위
	], // 아래
];

const [W] = input.shift();
const wall = Array.from(Array(N), () => Array(M).fill(0));
for (let i = 0; i < W; i++) {
	let [x, y, t] = input.shift();
	x = x - 1;
	y = y - 1;
	if (t == 0) {
		wall[x][y] += 4; // 위에 벽
		wall[x - 1][y] += 8; // 아래 벽
	} else {
		wall[x][y] += 1; // (x y) 오른쪽에 벽
		//(x, y+1) 왼쪽에 벽
		wall[x][y + 1] += 2;
	}
}

const targets = [];
const hitters = [];

for (let i = 0; i < N; i++) {
	for (let j = 0; j < M; j++) {
		if (board[i][j] > 0 && board[i][j] < 5) {
			hitters.push([i, j, board[i][j] - 1]);
			board[i][j] = 0;
		} else if (board[i][j] == 5) {
			targets.push([i, j]);
			board[i][j] = 0;
		}
	}
}

// console.log(hitters);
// console.log(targets);
//// 정리 끝

let choco = 0;

while (true) {
	// 온풍기 동시 작동.
	hitters.forEach((hitter) => {
		const [hx, hy, z] = hitter;
		const next = d[z];
		const sx = hx + next[1][0];
		const sy = hy + next[1][1];

		let visited = Array.from(Array(N), () => Array(M).fill(false));
		board[sx][sy] += 5;
		visited[sx][sy] = true;
		const q = new Queue();
		q.push([sx, sy, 5]);
		while (q.length > 0) {
			const [x, y, t] = q.pop();
			if (t == 1) continue;
			for (let k = 0; k < 3; k++) {
				const nx = x + next[k][0];
				const ny = y + next[k][1];
				const chkw = next[k][2];

				if (nx < 0 || ny < 0 || nx >= N || ny >= M || visited[nx][ny]) continue;
				if (chkw(x, y, nx, ny)) continue;

				visited[nx][ny] = true;
				board[nx][ny] += t - 1;
				q.push([nx, ny, t - 1]);
			}
		}
		// console.log(board.map((v) => v.join(' ')).join('\n'));
		// process.exit();
	});

	// 온도의 평등
	let newBoard = board.map((v) => [...v]);

	for (let x = 0; x < N; x++) {
		for (let y = 0; y < M; y++) {
			const T = board[x][y];

			for (let k = 0; k < 4; k++) {
				const nx = x + dx[k];
				const ny = y + dy[k];
				const chkw = dw[k];
				if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
				if (chkw(x, y)) continue;
				const otherT = board[nx][ny];
				const diff = Math.abs(T - otherT);
				const q = Math.floor(diff / 4);
				if (q > 0) {
					if (T > otherT) {
						newBoard[x][y] -= q;
						if (newBoard[x][y] < 0) newBoard[x][y] = 0;
					} else {
						newBoard[x][y] += q;
					}
				}
			}
		}
	}
	board = newBoard;

	// console.log('===================');
	// console.log(board.map((v) => v.join(' ')).join('\n'));

	for (let i = 0; i < N; i++) {
		if (i == 0 || i == N - 1) {
			for (let j = 0; j < M; j++) {
				if (board[i][j] > 0) board[i][j]--;
			}
		} else {
			for (let j = 0; j < M; j += M - 1) {
				if (board[i][j] > 0) board[i][j]--;
			}
		}
	}
	// 초콜릿 먹기
	choco++;
	// 모든 조사칸의 온도가 K 이상 이면 중단.
	if (choco > 100 || !targets.some((v) => board[v[0]][v[1]] <= K - 1)) {
		// console.log(board.map((v) => v.join(' ')).join('\n'));
		console.log(choco);
		break;
	}
}
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
글 보관함