๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

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

๋ฟŒ์•ผ._. 2022. 12. 20. 14:09

Gold I

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

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

 

๋ฌธ์ œ ํ’€์ด

 

bfs๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ตฌ์Šฌ์„ ์ƒํ•˜์ขŒ์šฐ๋กœ ์›€์ง์ธ๋‹ค. ์ด๋™ํ•œ ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๊ฐ™์€ ์œ„์น˜๋ผ๋ฉด ์œ„์น˜ ์กฐ์ •์„ ํ•ด์ค€๋‹ค. 

๊ฐ™์€ ์œ„์น˜๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๊ฐ Queue์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์ตœ์†Œ ํšŸ์ˆ˜๋ฅผ ์ฐพ์œผ๋ฉด ๋ฐ”๋กœ ํƒˆ์ถœ ํ›„ ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

 - 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 _13459_ {
	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 = 0;
		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=0;
						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=1;
								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==1)break;
			}
			if(result==1)break;
		}
	}
}

 

  • ๋ณ€์ˆ˜ ์„ค๋ช…
    • map: ๋ณด๋“œ 
    • R, B: ๊ฐ๊ฐ ๋นจ๊ฐ„ ๊ตฌ์Šฌ, ํŒŒ๋ž€ ๊ตฌ์Šฌ์˜ [x ์ขŒํ‘œ, y ์ขŒํ‘œ, count]
    • result: ์ตœ์†Œ ํšŸ์ˆ˜
    • check: ๋นจ๊ฐ„ ๊ตฌ์Šฌ์ด ๊ตฌ๋ฉ์— ๋“ค์–ด๊ฐ„ ์—ฌ๋ถ€ ํ™•์ธ
  • main
    • ๋ณด๋“œ ์ž…๋ ฅ ํ›„ ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์˜ ์œ„์น˜๋ฅผ ๊ฐ queue์— ์ €์žฅ
    • search ํ•จ์ˆ˜ ํ˜ธ์ถœ
    • result ์ถœ๋ ฅ
  • search
    • ๋นจ๊ฐ„ ๊ตฌ์Šฌ queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    • ๊ฐ๊ฐ ๊ตฌ์Šฌ queue์—์„œ ํ•˜๋‚˜์”ฉ ๋ฝ‘์€ ํ›„ ์ƒํ•˜์ขŒ์šฐ ์ด๋™
      • ๋นจ๊ฐ„ ๊ตฌ์Šฌ์„ ์›€์ง์ด๋ฉฐ ๋” ์ด์ƒ ์ด๋™ํ•  ์ˆ˜ ์—†๋Š” ์œ„์น˜๋ผ๋ฉด ํƒˆ์ถœ
      • ๊ตฌ๋ฉ์— ๋น ์กŒ๋‹ค๋ฉด check๋ฅผ true๋กœ ๋ฐ”๊พผ ํ›„ ํƒˆ์ถœ
      • ํŒŒ๋ž€ ๊ตฌ์Šฌ์„ ์›€์ง์ด๋ฉฐ ๊ตฌ๋ฉ์— ๋น ์กŒ๋‹ค๋ฉด result๋ฅผ 0์œผ๋กœ ๋ฐ”๊พผ ํ›„ ํƒˆ์ถœ
      • ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๋” ์ด์ƒ ์ด๋™ํ•  ์ˆ˜ ์—†๋Š” ์œ„์น˜์ด๊ณ  ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ ์ขŒํ‘œ๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๊ฐ๊ฐ ์œ„์น˜ ์กฐ์ •(์˜ˆ๋ฅผ ๋“ค์–ด ์œ„๋กœ ์›€์ง์ธ ๊ฒฝ์šฐ ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๋” ์œ„์— ์žˆ๋‹ค๋ฉด ํŒŒ๋ž€ ๊ตฌ์Šฌ๋ณด๋‹ค ๋นจ๊ฐ„ ๊ตฌ์Šฌ์ด ํ•œ ์นธ ๋” ๋ฐ‘์— ์žˆ๋„๋ก ์กฐ์ •) but ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ ์ขŒํ‘œ๊ฐ€ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด ๋นจ๊ฐ„ ๊ตฌ์Šฌ์ด ๊ตฌ๋ฉ์— ๋น ์ง„ ์ง€ ํ™•์ธ ํ›„ ๋น ์กŒ์œผ๋ฉด ํƒˆ์ถœ! ๊ตฌ๋ฉ์— ๋น ์ง€์ง€ ์•Š์•˜๋‹ค๋ฉด ์ด๋™ํ•˜๊ธฐ ์ „ ์œ„์น˜์™€ ๋น„๊ตํ•ด์„œ ๊ฐ™์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋งŒ queue์— ์ €์žฅ

์ƒ๊ฐ๐Ÿค”

 

๊ตฌ์Šฌ ํƒˆ์ถœ 2 ๋ฌธ์ œ์™€ ๋˜‘๊ฐ™์ด ํ’€๋ ค๋‹ค๊ฐ€ ์‹œ๊ฐ„์„ ์ค„์ด๊ณ  ์‹ถ์–ด์„œ ๋‹ค๋ฅธ ๋ฐฉํ–ฅ์œผ๋กœ ํ’€์—ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ์‹œ๊ฐ„์„ ๋‹จ์ถ•ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 


'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 12919_A์™€ B 2  (0) 2022.12.22
[Baekjoon] 15644_๊ตฌ์Šฌ ํƒˆ์ถœ 3  (0) 2022.12.21
[Baekjoon] 13460_๊ตฌ์Šฌ ํƒˆ์ถœ 2  (0) 2022.12.19
[Baekjoon] 4179_๋ถˆ!  (0) 2022.12.16
[Baekjoon] 16953_A โ†’ B  (0) 2022.12.14