๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1331_๋‚˜์ดํŠธ ํˆฌ์–ด

๋ฟŒ์•ผ._. 2024. 5. 16. 11:48

Silver IV

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/1331)

< ๋‚˜์ดํŠธ ํˆฌ์–ด >

 

๋ฌธ์ œ ํ’€์ด 

 

๋‚˜์ดํŠธ๊ฐ€ ๊ฒฝ๋กœ๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํŒ๋‹จํ•œ๋‹ค. ๋ชจ๋“  ์นธ์„ ์ •ํ™•ํžˆ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ณณ์—๋Š” ๊ฐˆ ์ˆ˜ ์—†๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class _1331_ { // ๋‚˜์ดํŠธ ํˆฌ์–ด
	static Queue<int[]> queue;
	static boolean arr[][];

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		arr = new boolean[6][6];

		int startX = 0, startY = 0;
		queue = new LinkedList<>();

		for (int i = 0; i < 36; i++) {
			String str = bf.readLine();
			int x = 0;
			int y = 6 - (str.charAt(1) - '0');

			if (str.charAt(0) == 'A') {
				x = 0;
			} else if (str.charAt(0) == 'B') {
				x = 1;
			} else if (str.charAt(0) == 'C') {
				x = 2;
			} else if (str.charAt(0) == 'D') {
				x = 3;
			} else if (str.charAt(0) == 'E') {
				x = 4;
			} else if (str.charAt(0) == 'F') {
				x = 5;
			}

			if (i == 0) {
				startX = x;
				startY = y;
			}
			queue.add(new int[] { x, y });
		}
		queue.add(new int[] { startX, startY });

		if (check(queue)) {
			System.out.println("Valid");
		} else {
			System.out.println("Invalid");
		}
	}

	private static boolean check(Queue<int[]> queue) {
		int dx[] = { -1, -2, -2, -1, 1, 2, 2, 1 };
		int dy[] = { -2, -1, 1, 2, 2, 1, -1, -2 };

		int now[] = queue.poll();
		boolean flag = false;

		while (!queue.isEmpty()) {
			for (int i = 0; i < 8; i++) {
				int x = now[0] + dx[i];
				int y = now[1] + dy[i];
				if (queue.peek()[0] == x && queue.peek()[1] == y) {
					if (arr[x][y]) {
						return false;
					}
					arr[x][y] = true;
					flag = true;
					now = queue.poll();
					break;
				}
			}
			if (!flag) {
				return false;
			}
			flag = false;
		}
		return true;
	}
}
๋ณ€์ˆ˜)
arr : ์ฒด์ŠคํŒ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
startX, startY : ์‹œ์ž‘ ์œ„์น˜
queue : ๋‚˜์ดํŠธ ๋ฐฉ๋ฌธ ๊ฒฝ๋กœ
str : ๋ฐฉ๋ฌธ ์œ„์น˜
x, y : ๋ฐฉ๋ฌธ ์œ„์น˜๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜

 

36 ๊ฐœ์ค„์— ๊ฑธ์ณ ๋‚˜์ดํŠธ์˜ ๋ฐฉ๋ฌธ ์œ„์น˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. A-F๋ฅผ ๊ฐ 0-5๋กœ ์น˜ํ™˜ํ•˜๊ณ  1-6์„ 5-0์œผ๋กœ ์น˜ํ™˜ํ•˜์—ฌ queue์— ์ €์žฅํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฌธ ์œ„์น˜๋ฅผ ๊ธฐ์–ตํ•ด ๋’€๋‹ค ๋งˆ์ง€๋ง‰์— ๋‹ค์‹œ queue์— ์ถ”๊ฐ€ํ•œ๋‹ค. ๊ทธ ํ›„ check ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋‚˜์ดํŠธ์˜ ๊ฒฝ๋กœ๋ฅผ ํ™•์ธํ•˜๊ณ  ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์ด๋ฉด Valid๋ฅผ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒฝ๋กœ๋ฉด Invalid๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

check (Queue <int []> queue)

queue์—์„œ poll ํ•˜์—ฌ ์‹œ์ž‘ ์œ„์น˜๋ฅผ now์— ์ €์žฅํ•œ๋‹ค. queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

1) ๋‚˜์ดํŠธ๊ฐ€ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” 8๋ฐฉํ–ฅ์˜ ์ขŒํ‘œ๋ฅผ ๊ตฌํ•œ ํ›„ ๊ทธ ์ขŒํ‘œ๊ฐ€ queue์˜ peek ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

2) ์ผ์น˜ํ•˜๋ฉด ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์‚ดํŽด๋ณธ๋‹ค. ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ณณ์ด๋ผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์ด๋ผ๋ฉด ๋ฐฉ๋ฌธ ํ‘œ์‹œ ํ›„ queue์—์„œ poll ํ•œ ๊ฐ’์œผ๋กœ ํ˜„์žฌ ์œ„์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•œ ํ›„ ๋‹ค์‹œ 1)๋กœ ๋Œ์•„๊ฐ„๋‹ค.

3) 8๋ฐฉํ–ฅ ์ขŒํ‘œ ๋ชจ๋‘ queue์˜ peek ๊ฐ’๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š๋‹ค๋ฉด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๋‚˜์ดํŠธ ๊ฒฝ๋กœ์ด๋ฏ€๋กœ false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

queue๊ฐ€ ๋น„์—ˆ๋‹ค๋ฉด ๋‚˜์ดํŠธ์˜ ์˜ฌ๋ฐ”๋ฅธ ๊ฒฝ๋กœ์ด๋ฏ€๋กœ true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.