๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1726_๋กœ๋ด‡

๋ฟŒ์•ผ._. 2023. 8. 8. 14:19

Gold III

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

< ๋กœ๋ด‡ >

 

๋ฌธ์ œ ํ’€์ด 

 

๋จผ์ € ํ˜„์žฌ ์œ„์น˜์—์„œ ๋™์„œ๋‚จ๋ถ์œผ๋กœ ์ด๋™ํ•ด ๋ณธ๋‹ค. ํ•œ ๋ฒˆ ์ด๋™ํ•  ๋•Œ ์ตœ๋Œ€ 3์นธ ์ด๋™ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ 1์นธ, 2์นธ, 3์นธ์„ ์ด๋™์‹œ์ผœ ๋ณธ๋‹ค. ์ค‘๊ฐ„์— 1์ด ์žˆ์œผ๋ฉด ์ด๋™ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค. 

 

๋งŒ์•ฝ ๋™์„œ๋‚จ๋ถ์œผ๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ํ˜„์žฌ ๋ฐฉํ–ฅ์—์„œ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•˜๋ฏ€๋กœ ๋ช…๋ น์˜ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•ด์ค€๋‹ค.

์ด๋•Œ, ๋ฐฉํ–ฅ ๋ฒˆํ˜ธ๋กœ๋งŒ ๋ช…๋ น์˜ ์ˆ˜๋ฅผ ๊ตฌํ•  ๊ฒฝ์šฐ ๋™์—์„œ ๋ถ์œผ๋กœ ๊ฐ€๋Š” ๊ฒฝ์šฐ 2๋ฒˆ์œผ๋กœ ๊ตฌํ•ด์ง€์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” 1๋ฒˆ์ธ ๊ฒƒ์„ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค.

 

์ด๋™ํ•œ ๊ณณ์ด ์ตœ์ข… ์›ํ•˜๋Š” ์œ„์น˜๋ผ๋ฉด ์›ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐ”๋ผ๋ณด๋„๋ก ๊ณ„์‚ฐํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ตœ์†Œ ๋ช…๋ น ํšŸ์ˆ˜๋ผ๋Š” ๋ณด์žฅ์ด ์—†์œผ๋ฏ€๋กœ ์ค‘๊ฐ„์— ๊ทธ๋งŒ๋‘์ง€ ์•Š๊ณ  ์ „์ฒด ๋‹ค ๊ณ„์‚ฐ ํ›„ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์•„์ค€๋‹ค.

 

์ด๋™ํ•œ ๊ณณ์ด ์ตœ์ข… ์›ํ•˜๋Š” ์œ„์น˜๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๊ทธ ์œ„์น˜์— ๋ฐฉ๋ฌธํ•œ ์ ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋ฐฉ๋ฌธ์„ ํ•œ ์ ์ด ์—†๊ฑฐ๋‚˜, ๋ฐฉ๋ฌธ์„ ํ–ˆ์—ˆ์ง€๋งŒ ๋ช…๋ น์˜ ํšŸ์ˆ˜๊ฐ€ ๊ทธ๋•Œ๋ณด๋‹ค ์ ๋‹ค๋ฉด 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 _1726_ { // ๋กœ๋ด‡
	static int arr[][], visited[][], m, n, x2, y2, dir2;
	static int dx[] = { 0, 0, 1, -1 };
	static int dy[] = { 1, -1, 0, 0 };

	public static class Robot {
		int x, y, dir, depth;

		public Robot(int x, int y, int dir, int depth) {
			this.x = x;
			this.y = y;
			this.dir = dir;
			this.depth = depth;
		}
	}

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

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

		arr = new int[m][n];
		visited = new int[m][n];

		for (int i = 0; i < m; i++) {
			st = new StringTokenizer(bf.readLine());
			for (int j = 0; j < n; j++) {
				arr[i][j] = Integer.parseInt(st.nextToken());
				visited[i][j] = -1;
			}
		}

		st = new StringTokenizer(bf.readLine());
		int x1 = Integer.parseInt(st.nextToken()) - 1;
		int y1 = Integer.parseInt(st.nextToken()) - 1;
		int dir1 = Integer.parseInt(st.nextToken()) - 1;

		st = new StringTokenizer(bf.readLine());
		x2 = Integer.parseInt(st.nextToken()) - 1;
		y2 = Integer.parseInt(st.nextToken()) - 1;
		dir2 = Integer.parseInt(st.nextToken()) - 1;

		int result = 0;

		if (x1 == x2 && y1 == y2) {
			result = dirCalc(dir2, dir1, 0);
		} else {
			result = bfs(x1, y1, dir1);
		}

		System.out.println(result);
	}

	private static int bfs(int a, int b, int dir) {
		Queue<Robot> queue = new LinkedList<>();
		queue.add(new Robot(a, b, dir, 0));
		visited[a][b] = 0;
		boolean flag = false;
		int cnt = -1;

		while (!queue.isEmpty()) {
			Robot robot = queue.poll();
			for (int i = 0; i < 4; i++) {
				flag = false;
				for (int j = 1; j <= 3; j++) {
					int x = robot.x + (dx[i] * j);
					int y = robot.y + (dy[i] * j);
					if (x >= 0 && x < m && y >= 0 && y < n && arr[x][y] == 0) {
						flag = true;
						int depth = dirCalc(robot.dir, i, robot.depth);
						Robot newRobot = new Robot(x, y, i, depth + 1);
						if (x == x2 && y == y2) {
							depth = dirCalc(dir2, i, depth) + 1;
							if (cnt == -1 || cnt > depth) {
								cnt = depth;
							}
						}
						if (visited[x][y] == -1 || visited[x][y] > newRobot.depth) {
							visited[x][y] = newRobot.depth;
							queue.add(newRobot);
						}
					} else {
						flag = false;
					}
					if (!flag)
						break;
				}
			}
		}
		return cnt;
	}

	public static int dirCalc(int x, int y, int depth) {
		if ((x == 2 && y == 3) || (x == 3 && y == 2) || (x == 1 && y == 0) || (x == 0 && y == 1)) {
			depth += 2;
		} else if (x != y) {
			depth += 1;
		}
		return depth;
	}
}

 

Main

๋ณ€์ˆ˜)
m, n : ์„ธ๋กœ, ๊ฐ€๋กœ๊ธธ์ด
arr : ๊ถค๋„ ์„ค์น˜ ์ƒํƒœ
visited : ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ์˜ ๋ช…๋ น ํšŸ์ˆ˜
x1, y1, dir1 : ํ˜„์žฌ ์œ„์น˜, ๋ฐฉํ–ฅ
x2, y2, dir2 : ์›ํ•˜๋Š” ์œ„์น˜, ๋ฐฉํ–ฅ
result : ์ตœ์†Œ ๋ช…๋ น ํšŸ์ˆ˜

 

- ์„ธ๋กœ ๊ธธ์ด(m), ๊ฐ€๋กœ๊ธธ์ด(n) ์ž…๋ ฅ

- ๊ถค๋„ ์„ค์น˜ ์ƒํƒœ ์ž…๋ ฅ ํ›„ ์ €์žฅ(arr) ๋ฐ ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ์˜ ๋ช…๋ น ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” visited ๋ฐฐ์—ด -1๋กœ ์ดˆ๊ธฐํ™”

- ํ˜„์žฌ ์œ„์น˜์™€ ๋ฐฉํ–ฅ, ์›ํ•˜๋Š” ์œ„์น˜์™€ ๋ฐฉํ–ฅ ์ž…๋ ฅ

- ํ˜„์žฌ ์œ„์น˜์™€ ์›ํ•˜๋Š” ์œ„์น˜๊ฐ€ ์ผ์น˜ํ•˜๋‹ค๋ฉด ๋ฐฉํ–ฅ๋งŒ ๊ณ„์‚ฐ/ ์ผ์น˜ํ•˜์ง€ ์•Š๋‹ค๋ฉด bfs ํ•จ์ˆ˜ ํ˜ธ์ถœ

- ์ตœ์†Œ ๋ช…๋ น ํšŸ์ˆ˜(result) ์ถœ๋ ฅ

 

bfs

-  Queue์— Robot <x ์œ„์น˜, y ์œ„์น˜, ๋ฐฉํ–ฅ, ๋ช…๋ น ํšŸ์ˆ˜> ์ €์žฅ

- ์‹œ์ž‘ ์œ„์น˜์˜ ๋ช…๋ น ํšŸ์ˆ˜๋Š” 0์ด๋ฏ€๋กœ visited์— 0์œผ๋กœ ์ €์žฅ

- queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

: ๋™์„œ๋‚จ๋ถ์œผ๋กœ ์ด๋™ -> 1์นธ, 2์นธ, 3์นธ ์ด๋™ -> ๋ฒ”์œ„ ์•ˆ์ด๋ฉฐ, ๊ถค๋„๊ฐ€ ๊น”๋ ค์žˆ๋‹ค๋ฉด

: dirCalc ํ˜ธ์ถœํ•˜์—ฌ ๊ทธ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น ์ˆ˜ ๊ณ„์‚ฐ

: Robot ๊ฐ์ฒด ๋งŒ๋“  ํ›„ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜, ๋ฐฉ๋ฌธ์„ ํ–ˆ์ง€๋งŒ ๋ช…๋ น ์ˆ˜๊ฐ€ ๋” ์ ๋‹ค๋ฉด queue์— ์ถ”๊ฐ€

: ๋งŒ์•ฝ ๋กœ๋ด‡์ด ์›ํ•˜๋Š” ์œ„์น˜์— ๋„๋‹ฌํ–ˆ๋‹ค๋ฉด ๋ฐฉํ–ฅ์„ ์ผ์น˜์‹œํ‚ค๊ธฐ ์œ„ํ•ด dirCalc ํ˜ธ์ถœํ•˜์—ฌ ๋ช…๋ น ์ˆ˜ ๊ณ„์‚ฐ ํ›„ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ๊ฐ’ update

: ๋งŒ์•ฝ 2์นธ ์ด๋™์„ ๋ชปํ•˜๋ฉด 3์นธ๋„ ์ด๋™ ๋ชปํ•˜๋ฏ€๋กœ flag๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒ๋‹จ

- cnt ๋ฐ˜ํ™˜

 

dirCalc

- (๋™-์„œ), (๋‚จ-๋ถ) ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด ๋ช…๋ น+2 ๋ฐ˜ํ™˜, ๋‚˜๋จธ์ง€๋Š” ๋ช…๋ น+1 ๋ฐ˜ํ™˜

 

 



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

[Baekjoon] 26042_์‹๋‹น ์ž…๊ตฌ ๋Œ€๊ธฐ ์ค„  (0) 2023.08.10
[Baekjoon] 28107_ํšŒ์ „์ดˆ๋ฐฅ  (0) 2023.08.09
[Baekjoon] 5464_์ฃผ์ฐจ์žฅ  (0) 2023.08.08
[Baekjoon] 11559_Puyo Puyo  (0) 2023.08.07
[Baekjoon] 15828_Router  (0) 2023.08.07