๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 4179_๋ถˆ!

๋ฟŒ์•ผ._. 2022. 12. 16. 12:03

Gold IV

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

<๋ถˆ!>

 

๋ฌธ์ œ ํ’€์ด

 

bfs๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ถˆ์„ ๋จผ์ € ์ด๋™์‹œํ‚จ ํ›„ ์‚ฌ๋žŒ์„ ์ด๋™์‹œํ‚จ๋‹ค.

 

 - my solution (Java)

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

public class _4179_ { // ๋ถˆ!
	static boolean flag ;
	static char map[][];
	static Queue<int[]> queue, position;
	static int dx[] = { -1, 1, 0, 0 }; // ์ƒํ•˜์ขŒ์šฐ
	static int dy[] = { 0, 0, -1, 1 };
	static int r, c, result;

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

		r = Integer.parseInt(st.nextToken());
		c = Integer.parseInt(st.nextToken());

		map = new char[r][c];
		queue = new LinkedList<>();

		position = new LinkedList<>();
		flag = false;
		for (int i = 0; i < r; i++) { // input
			String s = bf.readLine();
			for (int j = 0; j < c; j++) {
				map[i][j] = s.charAt(j);
				if (map[i][j] == 'J') {
					position.add(new int[] { i, j, 0 });
				} else if (map[i][j] == 'F') {
					queue.add(new int[] { i, j, 0 });
				}
			}
		}
		result = 0;

		while (true) {
			if (position.size() == 0)
				break;
			result += 1;
			search();
			if (flag)
				break;
		}

		if (flag)
			System.out.println(result);
		else
			System.out.println("IMPOSSIBLE");
	}

	private static void search() {
		while (!queue.isEmpty()) { // fire move
			if(queue.peek()[2]==result-1) {
				int[] temp = queue.poll();
				for (int i = 0; i < 4; i++) {
					int x = temp[0] + dx[i];
					int y = temp[1] + dy[i];
					if (x >= 0 && x < r && y >= 0 && y < c) {
						if (map[x][y] == '.' || map[x][y] == 'J') {
							map[x][y] = 'F';
							queue.add(new int[] { x, y, temp[2] + 1 });
						}
					}
				}
			}else break;
		}
		while (!position.isEmpty() ) { // ์ง€ํ›ˆ move
			if(position.peek()[2]==result-1) {
				int[] temp = position.poll();
				for (int i = 0; i < 4; i++) {
					int x = temp[0] + dx[i];
					int y = temp[1] + dy[i];
					if (x >= 0 && x < r && y >= 0 && y < c && map[x][y] == '.') {
						position.add(new int[] { x, y, temp[2] + 1 });
						map[x][y]='J';
					} else if (x < 0 || x >= r || y < 0 || y >= c) { // ๋ฏธ๋กœ ํƒˆ์ถœ
						flag = true;
						position.clear();
						break;
					}
				}
				if(flag) break;
			}else break;
		}
	}
}

 

  •  Main
    • r, c ์ž…๋ ฅ
    • map : ๋ฏธ๋กœ ์ž…๋ ฅ
    • queue, position : ๋ถˆ์˜ ์œ„์น˜์™€ ์‚ฌ๋žŒ ์œ„์น˜๋ฅผ [x์ขŒํ‘œ, y์ขŒํ‘œ, ์ด๋™ ํšŸ์ˆ˜] ๋ฐฐ์—ด๋กœ ์ €์žฅ
    • flag: ์ข…๋ฃŒ ์กฐ๊ฑด (์‚ฌ๋žŒ์ด ๋ฏธ๋กœ๋ฅผ ํƒˆ์ถœํ•œ ๊ฒฝ์šฐ)
    • ๋ฏธ๋กœ๋ฅผ ์ž…๋ ฅ ๋ฐ›์Œ๊ณผ ๋™์‹œ์— ์‚ฌ๋žŒ ์œ„์น˜์™€ ๋ถˆ ์œ„์น˜๋ฅผ ๊ฐ๊ฐ Queue์— ์ €์žฅ
    • ๋ฌดํ•œ ๋ฐ˜๋ณต : ์ข…๋ฃŒ ์กฐ๊ฑด ) ์‚ฌ๋žŒ์ด ๋ฏธ๋กœ๋ฅผ ํƒˆ์ถœํ–ˆ๊ฑฐ๋‚˜ ๋ถˆ์ด ๋„๋‹ฌํ•˜์—ฌ ๋ฏธ๋กœ๋ฅผ ํƒˆ์ถœํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ
    • ์‚ฌ๋žŒ์ด ๋ฏธ๋กœ๋ฅผ ํƒˆ์ถœํ•œ ๊ฒฝ์šฐ flag๊ฐ€ true์ด๋ฏ€๋กœ result๋ฅผ ์ถœ๋ ฅ but flag๊ฐ€ false์ด๋ฉด ๋ฏธ๋กœ๋ฅผ ํƒˆ์ถœํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ IMPOSSIBLE ์ถœ๋ ฅ
  • search
    • ๋ถˆ ์ด๋™
      • 1์ดˆ์— ํ•ด๋‹นํ•˜๋Š” ๋ถˆ๋งŒ ์ด๋™์‹œํ‚ค๊ธฐ ์œ„ํ•ด queue์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด result-1 ๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๋ฝ‘์•„ ์ƒํ•˜์ขŒ์šฐ ์ด๋™ -> ๋ฏธ๋กœ ๋ฒ”์œ„ ์•ˆ์ด๋ฉฐ ์ง€๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„ ๋˜๋Š” ์‚ฌ๋žŒ์˜ ์œ„์น˜๋ผ๋ฉด ๋ถˆ๋กœ ํ‘œ์‹œ ๋ฐ queue์— ์ถ”๊ฐ€ (* ์‚ฌ๋žŒ์˜ ์œ„์น˜๋ผ๋„ ๋ถˆ๋กœ ํ‘œ์‹œํ•˜๋Š” ์ด์œ ๋Š” ์ด๋ฏธ position์ด๋ผ๋Š” queue์— ์‚ฌ๋žŒ์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•ด๋‘์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๊ด€์—†์Œ)
      • queue์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด result-1๊ณผ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด 1์ดˆ์— ํ•ด๋‹นํ•˜๋Š” ๋งŒํผ ๋ถˆ์„ ๋‹ค ์ด๋™์‹œ์ผฐ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ break
    • ์‚ฌ๋žŒ ์ด๋™
      • 1์ดˆ์— ํ•ด๋‹นํ•˜๋Š” ๋งŒํผ ์ง€ํ›ˆ์ด๋ฅผ ์ด๋™์‹œํ‚ค๊ธฐ ์œ„ํ•ด queue์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด result-1 ๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๋ฝ‘์•„ ์ƒํ•˜์ขŒ์šฐ ์ด๋™ -> ๋ฏธ๋กœ ๋ฒ”์œ„ ์•ˆ์ด๋ฉฐ ์ง€๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์ด๋ผ๋ฉด queue์— ์ €์žฅ ๋ฐ map์— ํ‘œ์‹œ
      • ์ƒํ•˜์ขŒ์šฐ ์ด๋™์‹œ ๋ฏธ๋กœ ๋ฒ”์œ„ ๋ฐ–์ด๋ผ๋ฉด ๋ฏธ๋กœ ํƒˆ์ถœ์ด๋ผ๋Š” ๋œป์ด๋ฏ€๋กœ flag๋ฅผ true๋กœ ๋ฐ”๊พผ ํ›„ break
      • queue์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด result-1 ๊ฐ’๊ณผ ๊ฐ™์ง€ ์•Š์œผ๋ฉด 1์ดˆ์— ํ•ด๋‹นํ•˜๋Š” ๋งŒํผ ์‚ฌ๋žŒ์ด ์ด๋™ํ–ˆ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ break

์ƒ๊ฐ๐Ÿค”

 

์ฒ˜์Œ์— ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ ์ด์œ ๋Š” ๋งค๋ฒˆ ๊ณ„์†ํ•ด์„œ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ถˆ์˜ ์œ„์น˜์™€ ์‚ฌ๋žŒ์˜ ์œ„์น˜๋ฅผ ๊ฐ queue์— ๋„ฃ์–ด์ฃผ๋Š” ์ž‘์—…์„ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด์—ˆ๋‹ค. ์ด ๋ถ€๋ถ„์„ ๋ฐ”๊พธ๋‹ˆ ์ด๋ฒˆ์—๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค. ์ด ๋ถ€๋ถ„์€ ์‚ฌ๋žŒ์ด ์ด๋™ํ•  ๋•Œ map์„ ์ง€๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์œผ๋กœ ๋‹ค์‹œ ๋ฐ”๊ฟ”์คŒ์œผ๋กœ์จ ๋™์ž‘์ด ์ค‘๋ณต๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•˜์˜€๋‹ค. ์ด ๋ถ€๋ถ„์„ ๊ณ ์น˜๊ณ  ์กฐ๊ฑด์„ ์กฐ๊ธˆ ๋” ๊ณ ์น˜๋‹ˆ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.