๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 13460_๊ตฌ์Šฌ ํƒˆ์ถœ 2

๋ฟŒ์•ผ._. 2022. 12. 19. 21:35

Gold I

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

<๊ตฌ์Šฌ ํƒˆ์ถœ 2>

 

๋ฌธ์ œ ํ’€์ด

 

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 _13460_ {
	static int dx[] = { -1, 1, 0, 0 };
	static int dy[] = { 0, 0, -1, 1 };
	static Queue<int[]> R, B;
	static char map[][];
	static int result = -1, x1, x2, y1, y2;
	static boolean flag, choice;

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

		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());

		map = new char[n][m];

		R = new LinkedList<>();
		B = new LinkedList<>();

		for (int i = 0; i < n; i++) { // input
			String s = bf.readLine();
			for (int j = 0; j < m; j++) {
				map[i][j] = s.charAt(j);
				if (map[i][j] == 'R') {
					R.add(new int[] { i, j, 0 });
				} else if (map[i][j] == 'B') {
					B.add(new int[] { i, j, 0 });
				}
			}
		}
		search();
		System.out.println(result);
	}

	private static void R_move(int temp, int dx, int dy) {
		while (true) {
			x1 += dx;
			y1 += dy;
			if (map[x1][y1] == '#' || (choice && x2 == x1+dx  && y2 == y1+dy  && map[x2][y2] != 'O')) {
				if(choice) {
					R.add(new int[] { x1 - dx, y1 - dy, temp });
					B.add(new int[] { x2 - dx, y2 - dy, temp });
				}
				break;
			} else if (map[x1][y1] == 'O') {
				result = temp;
				if(choice) flag=true;
				break;
			}
		}
	}

	private static void B_move(int temp, int dx, int dy) {
		while (true) {
			x2 += dx;
			y2 += dy;
			if (map[x2][y2] == '#' || (!choice && x1 == x2 + dx && y1 == y2 + dy && map[x1][y1] != 'O')) {
				if (!choice) {
					if (result != -1)
						break;
					R.add(new int[] { x1 - dx, y1 - dy, temp });
					B.add(new int[] { x2 - dx, y2 - dy, temp });
					break;
				}else if(map[x1][y1]=='O') {
					flag=true;
					break;
				}else {
					break;
				}
			} else if (map[x2][y2] == 'O') {
				if(choice) flag=true;
				result = -1;
				break;
			}
		}
	}

	private static void search() {

		while (!R.isEmpty()) {
			int r_temp[] = R.poll();
			int b_temp[] = B.poll();

			int temp = 0;
			flag = false; 

			for (int i = 0; i < 4; i++) {

				x1 = r_temp[0];
				y1 = r_temp[1];
				x2 = b_temp[0];
				y2 = b_temp[1];
				choice=false;
				
				temp = r_temp[2];
				temp += 1;
				if(temp>10) break;
				if (i == 0) { // up
					if (r_temp[1] == b_temp[1] && r_temp[0] > b_temp[0]) { // ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๋” ์œ„์— ์žˆ๋‹ค๋ฉด
						choice=true;
						B_move(temp, dx[i], dy[i]);
						if(flag)continue;
						R_move(temp, dx[i], dy[i]);
					} else {
						R_move(temp, dx[i], dy[i]);
						B_move(temp, dx[i], dy[i]);
					}
				} else if (i == 1) { // down
					if (r_temp[1] == b_temp[1] && r_temp[0] < b_temp[0]) {
						choice=true;
						B_move(temp, dx[i], dy[i]);
						if(flag)continue;
						R_move(temp, dx[i], dy[i]);
					} else {
						R_move(temp, dx[i], dy[i]);
						B_move(temp, dx[i], dy[i]);
					}
				} else if (i == 2) { // left
					if (r_temp[0] == b_temp[0] && r_temp[1] > b_temp[1]) {
						choice=true;
						B_move(temp, dx[i], dy[i]);
						if(flag)continue;
						R_move(temp, dx[i], dy[i]);
					} else {
						R_move(temp, dx[i], dy[i]);
						B_move(temp, dx[i], dy[i]);
					}
				} else { // right
					if (r_temp[0] == b_temp[0] && r_temp[1] < b_temp[1]) {
						choice=true;
						B_move(temp, dx[i], dy[i]);
						if(flag)continue;
						R_move(temp, dx[i], dy[i]);
					} else {
						R_move(temp, dx[i], dy[i]);
						B_move(temp, dx[i], dy[i]);
					}
				}
				if(result!=-1)break;
			}
			if(result!=-1)break;
		}
	}
}

 

  •  Main
    • ๋ณด๋“œ(map)์˜ ์„ธ๋กœ, ๊ฐ€๋กœ ํฌ๊ธฐ n, m ๋ฐ ๋ณด๋“œ(map) ์ž…๋ ฅ
    • Queue R๊ณผ B๋Š” ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์„ ์ €์žฅํ•œ๋‹ค. [x์ขŒํ‘œ, y์ขŒํ‘œ, count]
    • search() : bfs ํƒ์ƒ‰
    •  
  • R_move(count, ์ƒํ•˜์ขŒ์šฐ ์ด๋™ ์ค‘ ํ•˜๋‚˜)
    • x์ขŒํ‘œ์™€ y์ขŒํ‘œ๋ฅผ ์ƒํ•˜์ขŒ์šฐ ์ค‘ ํ•˜๋‚˜๋กœ ๋ฌดํ•œ ์ด๋™
      • ์ด๋™ํ•  ์ขŒํ‘œ๊ฐ€ ๊ตฌ์Šฌ์ด ์ด๋™ํ•  ์ˆ˜ ์—†๋Š” ์œ„์น˜์ด์ง€๋งŒ ๋นจ๊ฐ„ ๊ตฌ์Šฌ์ด ๋จผ์ € ์›€์ง์ธ ๊ฒƒ์ด๋ผ๋ฉด ๊ทธ๋Œ€๋กœ ๋ฐ˜๋ณต๋ฌธ ํƒˆ์ถœ
      • ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๋จผ์ € ์›€์ง์˜€์œผ๋ฉฐ, ๋นจ๊ฐ„ ๊ตฌ์Šฌ์ด ๋‹ค์Œ์— ์ด๋™ํ•  ์œ„์น˜์™€ ์ขŒํ‘œ๊ฐ€ ๊ฐ™๋‹ค๋ฉด R, B์— ์ €์žฅ ๋ฐ ํƒˆ์ถœ
      • ๋งŒ์•ฝ ๋นจ๊ฐ„ ๊ตฌ์Šฌ์ด ๊ตฌ๋ฉ์— ๋“ค์–ด๊ฐ”๋‹ค๋ฉด result์— count๋ฅผ ์ €์žฅ ๋ฐ  ํƒˆ์ถœ but ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๋จผ์ € ์›€์ง์˜€๋‹ค๋ฉด flag๋ฅผ true๋กœ ์„ค์ •  
  • B_move(count, ์ƒํ•˜์ขŒ์šฐ ์ด๋™ ์ค‘ ํ•˜๋‚˜)
    • x์ขŒํ‘œ์™€ y์ขŒํ‘œ๋ฅผ ์ƒํ•˜์ขŒ์šฐ ์ค‘ ํ•˜๋‚˜๋กœ ๋ฌดํ•œ ์ด๋™
      • if
        • ์ด๋™ํ•  ์ขŒํ‘œ๊ฐ€ ๊ตฌ์Šฌ์ด ์ด๋™ํ•  ์ˆ˜ ์—†๋Š” ์œ„์น˜์ด์ง€๋งŒ ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๋จผ์ € ์›€์ง์ธ ๊ฒƒ์ด๋ผ๋ฉด ๊ทธ๋Œ€๋กœ ๋ฐ˜๋ณต๋ฌธ ํƒˆ์ถœ
        • ๋นจ๊ฐ„ ๊ตฌ์Šฌ์ด ๋จผ์ € ์›€์ง์˜€์œผ๋ฉฐ, ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๋‹ค์Œ์— ์ด๋™ํ•  ์œ„์น˜์™€ ์ขŒํ‘œ๊ฐ€ ๊ฐ™๋‹ค๋ฉด R, B์— ์ €์žฅ ๋ฐ ํƒˆ์ถœ
        • ๋นจ๊ฐ„ ๊ตฌ์Šฌ์ด ๋จผ์ € ์ด๋™ ํ›„ ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ์ด๋™ํ•  ์ขŒํ‘œ๊ฐ€ ๊ตฌ์Šฌ์ด ์ด๋™ํ•  ์ˆ˜ ์—†๋Š” ์œ„์น˜์ด์ง€๋งŒ ๋นจ๊ฐ„ ๊ตฌ์Šฌ์€ ์ด๋ฏธ ๊ตฌ๋ฉ์— ๋“ค์–ด๊ฐ”๋‹ค๋ฉด flag๋ฅผ true๋กœ ๋ฐ”๊พผ ํ›„ ํƒˆ์ถœ
        • ๊ทธ ์™ธ์—๋Š” ๊ทธ๋ƒฅ ํƒˆ์ถœ
      • else if
        • ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๊ตฌ๋ฉ์— ๋–จ์–ด์ ธ ๋ฒ„๋ ธ๋Š”๋ฐ ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๋จผ์ € ์›€์ง์ธ ๊ฑฐ๋ผ๋ฉด flag๋ฅผ true๋กœ ๋ฐ”๊ฟˆ. ํŒŒ๋ž€ ๊ตฌ์Šฌ์€ ๊ฐ™์ด ๋–จ์–ด์ง€๋ฉด ์•ˆ ๋˜๋ฏ€๋กœ resut๋ฅผ -1๋กœ ๋ฐ”๊พธ๊ณ  ํƒˆ์ถœ
  • search()
    • ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์„ queue์—์„œ ๋นผ๋‚ด ์˜ด
    • ์ƒํ•˜์ขŒ์šฐ ๋ฐ˜๋ณต
      • ๋จผ์ € ๊ตฌ์Šฌ์˜ ์œ„์น˜์™€ count๋ฅผ ์ €์žฅํ•ด์คŒ
      • 10๋ฒˆ ์ดํ•˜๋กœ ์›€์ง์—ฌ์„œ ๋นจ๊ฐ„ ๊ตฌ์Šฌ์„ ๋นผ๋‚ผ ์ˆ˜ ์—†๋‹ค๋ฉด ํƒˆ์ถœ
      • ๊ฐ๊ฐ ์ƒํ•˜์ขŒ์šฐ๋กœ ์›€์ง์ผ ๋•Œ ์–ด๋Š ๊ตฌ์Šฌ์ด ๋จผ์ € ์›€์ง์—ฌ์•ผ ํ•˜๋Š”์ง€ ํŒ๋‹จ ํ›„ ๊ฐ๊ฐ ๊ตฌ์Šฌ ์›€์ง์ด๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ
      • choice: ํŒŒ๋ž€ ๊ตฌ์Šฌ์„ ๋จผ์ € ์›€์ง์ผ ๊ฒฝ์šฐ true๋กœ ์ €์žฅ
      • flag: ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๊ตฌ๋ฉ์— ๋น ์ง„ ๊ฒฝ์šฐ true๋กœ ์ €์žฅ
    • ์ตœ์†Œ ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ result๊ฐ€ -1์ด ์•„๋‹ˆ๋ฉด (์ดˆ๊ธฐ๊ฐ’์ด ์•„๋‹ˆ๋ฉด) ํƒˆ์ถœ ํ›„ ์ถœ๋ ฅ

 

 

+ ์‹œ๊ฐ„ ๋‹จ์ถ• ์ฝ”๋“œ

   (์„ค๋ช…์€ https://melody-coding.tistory.com/267 ์ฐธ๊ณ  )

๋”๋ณด๊ธฐ
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 _13460_ {
	static char map[][];
	static Queue<int[]> R, B;
	static int result;
	static int dx[] = { -1, 1, 0, 0 };
	static int dy[] = { 0, 0, -1, 1 };

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

		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());

		map = new char[n][m];
		result = -1;
		R = new LinkedList<>();
		B = new LinkedList<>();
		for (int i = 0; i < n; i++) {
			String s = bf.readLine();
			for (int j = 0; j < m; j++) {
				map[i][j] = s.charAt(j);
				if (map[i][j] == 'R')
					R.add(new int[] { i, j, 0 });
				else if (map[i][j] == 'B')
					B.add(new int[] { i, j, 0 });
			}
		}
		search();
		System.out.println(result);
	}

	private static void search() {
		boolean check = false;

		while (!R.isEmpty()) {
			int[] R_temp = R.poll();
			int[] B_temp = B.poll();
			int x1, y1, x2, y2;
			for (int i = 0; i < 4; i++) {
				int temp = R_temp[2] + 1;
				if (temp > 10)
					break; // 10๋ฒˆ ์ดํ•˜๊ฐ€ ์•„๋‹ˆ๋ฉด x
				x1 = R_temp[0];
				y1 = R_temp[1];
				check = false;
				while (true) { // ๋นจ๊ฐ„ ๊ตฌ์Šฌ
					x1 += dx[i];
					y1 += dy[i];

					if (map[x1][y1] == '#') {
						break;
					}
					if (map[x1][y1] == 'O') { // ๊ตฌ๋ฉ
						check = true;
						break;
					}
				}
				x2 = B_temp[0];
				y2 = B_temp[1];
				while (true) { // ํŒŒ๋ž€ ๊ตฌ์Šฌ
					x2 += dx[i];
					y2 += dy[i];
					if (map[x2][y2] == 'O') { // ํŒŒ๋ž€๊ตฌ์Šฌ์ด  ๊ตฌ๋ฉ์— ๋น ์ง€๋ฉด ์‹คํŒจ
						result = -1;
						break;
					}
					if (map[x2][y2] == '#') {
						if (x1 == x2 && y1 == y2) { // ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ ์ขŒํ‘œ๊ฐ€ ๊ฐ™์œผ๋ฉด ์œ„์น˜ ์กฐ์ •
							if (i == 0) {
								if (B_temp[0] < R_temp[0]) {
									x2 -= dx[i];
									x1 = x2 - dx[i];
								} else {
									x1 -= dx[i];
									x2 = x1 - dx[i];
								}
							} else if (i == 1) {
								if (B_temp[0] < R_temp[0]) {
									x1 -= dx[i];
									x2 = x1 - dx[i];
								} else {
									x2 -= dx[i];
									x1 = x2 - dx[i];
								}
							} else if (i == 2) {
								if (B_temp[1] < R_temp[1]) {
									y2 -= dy[i];
									y1 = y2 - dy[i];
								} else {
									y1 -= dy[i];
									y2 = y1 - dy[i];
								}
							} else {
								if (B_temp[1] < R_temp[1]) {
									y1 -= dy[i];
									y2 = y1 - dy[i];
								} else {
									y2 -= dy[i];
									y1 = y2 - dy[i];
								}
							}
						} else {
							x1 -= dx[i];
							y1 -= dy[i];
							x2 -= dx[i];
							y2 -= dy[i];
							if (check) { // ์ตœ์†Œ ํšŸ์ˆ˜
								result = temp;
								break;
							}
						}
						if (!(R_temp[0] == x1 && R_temp[1] == y1 && B_temp[0] == x2 && B_temp[1] == y2)) { // ์›๋ž˜ ์œ„์น˜์™€ ๋™์ผํ•˜์ง€ ์•Š์œผ๋ฉด 
							R.add(new int[] { x1, y1, temp });
							B.add(new int[] { x2, y2, temp });
						}
						break;
					}
				}
				if (result > 0)
					break;
			}
			if (result > 0)
				break;
		}
	}
}

์ƒ๊ฐ๐Ÿค”

 

์ด ๋ฌธ์ œ... ์„ฑ๊ณต ๋ชปํ•  ๋ป”ํ–ˆ๋‹ค.. ํฌ๊ธฐํ•  ๋ป”ํ–ˆ๋‹ค..!

์ฒ˜์Œ์— ํ‹€๋ ธ์„ ๋•Œ๋Š” ํŒŒ๋ž€ ๊ณต์ด ๋จผ์ € ์›€์ง์ด๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ๋ชปํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ž˜์„œ ์ผ๋‹จ ํฌ๊ธฐํ•˜๊ณ  ๋‹ค์Œ ๋‚  ๋‹ค์‹œ ๋„์ „ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ณ„์†ํ•ด์„œ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๊ฐ€ ๋–ด๊ณ ... ๋ฐ˜๋ก€๋ฅผ ์ฐพ์•„.. ๋– ๋‚ฌ๋‹ค..  ์™œ ๋‚œ ๋ฐ˜๋ก€๋ฅผ ์ฐพ์•„๋‚ด์ง€ ๋ชปํ•˜๋Š”๊ฐ€..

ํž˜๋“ค๊ฒŒ ํž˜๋“ค๊ฒŒ ์ฐพ์•„์„œ.. ๊ฒจ์šฐ ํ†ต๊ณผํ–ˆ๋‹ค. ๊ฒจ์šฐ ํ†ต๊ณผํ•œ ๋งŒํผ ์‹œ๊ฐ„์€ ์—„์ฒญ๋‚ฌ๊ณ ... ์ผ๋‹จ ์˜ค๋Š˜์€ ์—ฌ๊ธฐ์„œ ๋งŒ์กฑ(?)ํ•ด์•ผ๊ฒ ๋‹ค.

 

 

+ 13459๋ฒˆ ๊ตฌ์Šฌ ํƒˆ์ถœ์„ ํ’€๋ฉฐ ์‹œ๊ฐ„์„ ๋‹จ์ถ•ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.