๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 16954_์›€์ง์ด๋Š” ๋ฏธ๋กœ ํƒˆ์ถœ

๋ฟŒ์•ผ._. 2023. 5. 24. 23:28

Gold III

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

< ์›€์ง์ด๋Š” ๋ฏธ๋กœ ํƒˆ์ถœ >

 

๋ฌธ์ œ ํ’€์ด

 

์ด ๋ฌธ์ œ๋Š” ๋ฒฝ์ด 1์ดˆ๋งˆ๋‹ค ์•„๋ž˜๋กœ ํ•œ ์นธ์”ฉ ๋‚ด๋ ค๊ฐ„๋‹ค๋Š” ๋ถ€๋ถ„์ด ์ค‘์š”ํ•˜๋‹ค.

1์ดˆ๋งˆ๋‹ค ์•„๋ž˜๋กœ ํ•œ ์นธ์”ฉ ์›€์ง์ด๋Š” ๋ถ€๋ถ„์„ ์‹ค์ œ๋กœ ๋ฐฐ์—ด์— ์ €์žฅํ•˜์ง€ ์•Š๊ณ  ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜์—ฌ ์ด๋™ํ•  ์œ„์น˜๊ฐ€ ๋นˆ์นธ์ธ์ง€ ๋ฒฝ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.

- ์ž์„ธํ•œ ํ’€์ด๋Š” ๋‹ค์Œ์— ์‹œ๊ฐ„์ด ๋‚  ๋•Œ ์ž‘์„ฑ

 

 

 - 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 _16954_ { // ์›€์ง์ด๋Š” ๋ฏธ๋กœ ํƒˆ์ถœ
	static boolean arr[][], visited[][], result;
	static int dx[] = { 0, -1, 1, 0, 0, -1, -1, 1, 1 };
	static int dy[] = { 0, 0, 0, -1, 1, -1, 1, -1, 1 };

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

		arr = new boolean[8][8];
		visited = new boolean[8][8];
		result = false;

		for (int i = 0; i < 8; i++) {
			String str = bf.readLine();
			for (int j = 0; j < 8; j++) {
				if (str.charAt(j) == '#') {
					arr[i][j] = true; // ๋ฒฝ
				}
			}
		}
		search(7, 0, 0);

		if (result)
			System.out.println(1);
		else
			System.out.println(0);
	}

	private static void search(int start_x, int start_y, int depth) {
		Queue<int[]> queue = new LinkedList<>();
		queue.add(new int[] { start_x, start_y, depth });

		while (!queue.isEmpty()) {
			int temp[] = queue.poll();
			for (int i = 0; i < 9; i++) {
				int x = temp[0] + dx[i];
				int y = temp[1] + dy[i];
				if (x >= 0 && x < 8 && y >= 0 && y < 8  ) {
					if(x-temp[2]>=0 && arr[x-temp[2]][y]) continue;

					if(i!=0 && visited[x][y]) continue;
					if (x - (temp[2]+1) >= 0) {
						if (!arr[x - (temp[2]+1)][y]) {
							if (x == 0 && y == 7) {
								result = true;
								break;
							}
							visited[x][y] = true;
							queue.add(new int[] { x, y, temp[2] + 1 });

						}
					} else {
						if (x == 0 && y == 7) {
							result = true;
							break;
						}
						visited[x][y] = true;
						queue.add(new int[] { x, y, temp[2] + 1 });
					}
				}
			}
			if (result)
				break;
		}
	}
}

 

  • Main
    • arr : ์ฒด์ŠคํŒ ์ƒํƒœ ์ž…๋ ฅ
    • visited : ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
    • search ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์™ผ์ชฝ ์•„๋žซ์นธ๋ถ€ํ„ฐ ์‹œ์ž‘
    • ๊ฒฐ๊ณผ ์ถœ๋ ฅ
  • search 
    • queue์— ์‹œ์ž‘ ์œ„์น˜์œ„์น˜์™€ ์ด๋™ ํšŸ์ˆ˜ ์ €์žฅ {x, y, depth} 
    • queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต -> ํ˜„์žฌ ์œ„์น˜, ์ƒํ•˜์ขŒ์šฐ, ๋Œ€๊ฐ์„  ์ด๋™ -> 1์ดˆ๋งˆ๋‹ค ๋ฒฝ์ด ์ด๋™ํ•˜๋Š” ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ์ด๋™ํ•˜๋ ค๋Š” ์œ„์น˜๊ฐ€ ๋นˆ์นธ์ด๋ผ๋ฉด ์ด๋™ํ•˜๊ธฐ

์ƒ๊ฐ๐Ÿค”