๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 10469_์‚ฌ์ด ๋‚˜์œ ์—ฌ์™•๋“ค

๋ฟŒ์•ผ._. 2025. 2. 21. 15:48
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/10469)

< ์‚ฌ์ด ๋‚˜์œ ์—ฌ์™•๋“ค >

 

๋ฌธ์ œ ํ’€์ด 

 

์ƒ, ํ•˜, ์ขŒ, ์šฐ, ๋Œ€๊ฐ์„ ์œผ๋กœ ์ œํ•œ ์—†์ด ์ด๋™ํ–ˆ์„ ๋•Œ ๋‹ค๋ฅธ ์—ฌ์™•์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

*์—ฌ์™•์ด 8๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š๋‹ค.

 

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 _10469_ { // ์‚ฌ์ด ๋‚˜์œ ์—ฌ์™•๋“ค

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

		boolean visited[][] = new boolean[8][8];
		Queue<int[]> queue = new LinkedList<>();

		for (int i = 0; i < 8; i++) {
			String str = bf.readLine();
			for (int j = 0; j < 8; j++) {
				if (str.charAt(j) == '*') {
					visited[i][j] = true;
					queue.add(new int[] { i, j });
				}
			}
		}

		int dx[] = { -1, 1, 0, 0, -1, -1, 1, 1 };
		int dy[] = { 0, 0, -1, 1, -1, 1, -1, 1 };
		boolean flag = false;

		if (queue.size() != 8) {
			flag = true;
		}

		while (!queue.isEmpty() && !flag) {
			int temp[] = queue.poll();
			for (int i = 0; i < 8; i++) {
				int x = temp[0], y = temp[1];
				while (x + dx[i] >= 0 && x + dx[i] < 8 && y + dy[i] >= 0 && y + dy[i] < 8) {
					x += dx[i];
					y += dy[i];
					if (visited[x][y]) {
						flag = true;
						break;
					}
				}
				if (flag) {
					break;
				}
			}
		}

		if (flag) {
			System.out.println("invalid");
		} else {
			System.out.println("valid");
		}
	}
}

 

๋ณ€์ˆ˜)
visited : ์—ฌ์™• ์กด์žฌ ์—ฌ๋ถ€
queue : ์—ฌ์™• ์œ„์น˜
str : ์ž…๋ ฅ
dx, dy : ์ƒ, ํ•˜, ์ขŒ, ์šฐ, ๋Œ€๊ฐ์„ 
flag : ์˜ฌ๋ฐ”๋ฅธ ํ•ด๋ฒ• ์—ฌ๋ถ€

 

8x8 ์ฒด์ŠคํŒ์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉฐ ์—ฌ์™•์ด ์œ„์น˜ํ•œ ๊ณณ์ด๋ฉด visited๋ฅผ true๋กœ, queue์— ์œ„์น˜๋ฅผ ์ €์žฅํ•œ๋‹ค. ๋งŒ์•ฝ queue์˜ ํฌ๊ธฐ๊ฐ€ 8์ด ์•„๋‹ˆ๋ผ๋ฉด ์—ฌ์™•์ด 8๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ flag๋ฅผ true๋กœ ์ €์žฅํ•œ๋‹ค. queue๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š๊ณ  flag๊ฐ€ false๋ผ๋ฉด ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

1) queue poll

2) 8๊ฐœ์˜ ๋ฐฉํ–ฅ์œผ๋กœ ์ฒด์ŠคํŒ ๋ฒ”์œ„ ์•ˆ์— ์žˆ์„ ๋•Œ๊นŒ์ง€ ์ด๋™

3) ๋งŒ์•ฝ ์ด๋™ ์œ„์น˜์— ๋‹ค๋ฅธ ์—ฌ์™•์ด ์žˆ๋‹ค๋ฉด flag๋ฅผ true๋กœ ์ €์žฅ ๋ฐ ์ข…๋ฃŒ

 

flag๊ฐ€ true๋ผ๋ฉด "invalid"๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  false๋ผ๋ฉด "valid"๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.