티스토리 뷰

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

 

30036번: INK

첫 번째 줄에 정수 $I$, $N$, $K$가 공백으로 구분되어 주어진다. $(1 \le I, N, K \le 100)$ 두 번째 줄에는 잉크 문자열이 주어진다. 세 번째 줄부터 $N$개의 줄에 걸쳐 $N \times N$ 크기의 스테이지가 주어진

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.trim());
const [I, N] = input.shift().split(' ').map(Number);
const INK_STR = input.shift();
const board = input.splice(0, N).map((v) => v.split(''));
const CMD = input[0].split('');
// @ 사각형
// . 빈칸
// 염색되지않은 장애물 #
// 염색된 장애물 알파벳

let x; // 사각형 행
let y; //  사각형 열
let m = 0; // 잉크 양
let d = 0; // 점프 커맨드 입력 횟수
const dx = [0, 0, -1, 1];
const dy = [-1, 1, 0, 0];
for (let i = 0; i < N; i++) {
	for (let j = 0; j < N; j++) {
		if (board[i][j] == '@') {
			x = i;
			y = j;
			board[i][j] = '.';
		}
	}
}

CMD.forEach((c) => {
	switch (c) {
		case 'U': {
			const nx = x - 1;
			if (nx >= 0 && nx < N && board[nx][y] == '.') {
				x = nx;
			}
			break;
		}
		case 'D': {
			const nx = x + 1;
			if (nx >= 0 && nx < N && board[nx][y] == '.') {
				x = nx;
			}
			break;
		}
		case 'L': {
			const ny = y - 1;
			if (ny >= 0 && ny < N && board[x][ny] == '.') {
				y = ny;
			}
			break;
		}
		case 'R': {
			const ny = y + 1;
			if (ny >= 0 && ny < N && board[x][ny] == '.') {
				y = ny;
			}
			break;
		}
		case 'j':
			m++;
			break;
		case 'J':
			d++;
			if (m == 0) break;
			const visited = Array.from(Array(N), () => Array(N).fill(false));
			visited[x][y] = true;
			const q = new Queue();
			q.push([x, y, 0]);
			const COLOR = INK_STR[(m * I + d - 1) % I];
			while (q.length > 0) {
				const [sx, sy, c] = q.pop();
				for (let k = 0; k < 4; k++) {
					const nx = sx + dx[k];
					const ny = sy + dy[k];
					if (nx < 0 || nx >= N) continue;
					if (ny < 0 || ny >= N) continue;
					if (visited[nx][ny]) continue;
					visited[nx][ny] = true;
					if (board[nx][ny] != '.') {
						board[nx][ny] = COLOR;
					}
					if (c + 1 < m) {
						q.push([nx, ny, c + 1]);
					}
				}
			}
			m = 0;
			break;
	}
});

board[x][y] = '@';
console.log(board.map((v) => v.join('')).join('\n'));
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
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
29 30
글 보관함