๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 6832_Maze

๋ฟŒ์•ผ._. 2024. 11. 22. 15:27
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/6832)

< Maze >

 

๋ฌธ์ œ ํ’€์ด 

 

bfs ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•˜์—ฌ 

+ : ์ƒํ•˜์ขŒ์šฐ
- : ์ขŒ์šฐ
| : ์ƒํ•˜
* : ์ด๋™ ๋ถˆ๊ฐ€

์— ๋งž๊ฒŒ ์ด๋™ํ•œ๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;

public class _6832_ { // Maze
	static int dx[] = { -1, 1, 0, 0 };
	static int dy[] = { 0, 0, -1, 1 };
	static char arr[][];
    
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
		int t = Integer.parseInt(bf.readLine());
		for (int i = 0; i < t; i++) {
			int r = Integer.parseInt(bf.readLine());
			int c = Integer.parseInt(bf.readLine());
            
			arr = new char[r][c];
			for (int j = 0; j < r; j++) {
				String str = bf.readLine();
				for (int k = 0; k < c; k++) {
					arr[j][k] = str.charAt(k);
				}
			}
			if (r == 1 && c == 1 && arr[0][0]!='*') {
				bw.write("1\n");
			} else {
				bw.write(bfs() + "\n");
			}
		}
		bw.flush();
	}
    
	private static int bfs() {
		Queue<int[]> queue = new LinkedList<>();
		queue.add(new int[] { 0, 0, 1 });
        
		boolean visited[][] = new boolean[arr.length][arr[0].length];
		visited[0][0] = true;
        
		while (!queue.isEmpty()) {
			int temp[] = queue.poll();
            
			if (arr[temp[0]][temp[1]] == '+') {
				for (int k = 0; k < 4; k++) {
					int x = temp[0] + dx[k];
					int y = temp[1] + dy[k];
					if (x >= 0 && x < arr.length && y >= 0 && y < arr[0].length && arr[x][y] != '*' && !visited[x][y]) {
						if (x == arr.length - 1 && y == arr[0].length - 1) {
							return temp[2] + 1;
						}
						visited[x][y] = true;
						queue.add(new int[] { x, y, temp[2] + 1 });
					}
				}
			} else if (arr[temp[0]][temp[1]] == '-') {
				for (int k = 2; k < 4; k++) {
					int x = temp[0] + dx[k];
					int y = temp[1] + dy[k];
					if (x >= 0 && x < arr.length && y >= 0 && y < arr[0].length && arr[x][y] != '*' && !visited[x][y]) {
						if (x == arr.length - 1 && y == arr[0].length - 1) {
							return temp[2] + 1;
						}
						visited[x][y] = true;
						queue.add(new int[] { x, y, temp[2] + 1 });
					}
				}
			} else if (arr[temp[0]][temp[1]] == '|') {
				for (int k = 0; k < 2; k++) {
					int x = temp[0] + dx[k];
					int y = temp[1] + dy[k];
					if (x >= 0 && x < arr.length && y >= 0 && y < arr[0].length && arr[x][y] != '*' && !visited[x][y]) {
						if (x == arr.length - 1 && y == arr[0].length - 1) {
							return temp[2] + 1;
						}
						visited[x][y] = true;
						queue.add(new int[] { x, y, temp[2] + 1 });
					}
				}
			}
		}
		return -1;
	}
}
๋ณ€์ˆ˜)
dx, dy : ์ด๋™ ๋ฐฉํ–ฅ
t : ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐœ์ˆ˜
r, c : ๋ฐฐ์—ด ํฌ๊ธฐ
visited : ๋ฐฉ๋ฌธ ์—ฌ๋ถ€

 

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜๋งŒํผ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

1) ๋ฐฐ์—ด ํฌ๊ธฐ ์ž…๋ ฅ

2) ๋ฐฐ์—ด ํฌ๊ธฐ๋งŒํผ ๋ฐฐ์—ด ์ •๋ณด ์ž…๋ ฅ๋ฐ›์•„ arr์— ์ €์žฅ

3) ๋ฐฐ์—ด ํฌ๊ธฐ๊ฐ€ 1x1์ด๊ณ  ๊ฐ’์ด *๋ผ๋ฉด -1 ์ถœ๋ ฅ, *๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด 1 ์ถœ๋ ฅ

4) ๋ฐฐ์—ด ํฌ๊ธฐ๊ฐ€ 1x1์ด ์•„๋‹ˆ๋ผ๋ฉด bfs ํ•จ์ˆ˜ ๋ฐ˜ํ™˜๊ฐ’ ์ถœ๋ ฅ

 

bfs()

Queue์— ์‹œ์ž‘ ์œ„์น˜์™€ ์ด๋™ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค. ์‹œ์ž‘ ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•œ๋‹ค. Queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

1) Queue poll

2) ํ˜„์žฌ ๊ฐ’์ด +๋ผ๋ฉด ์ƒํ•˜์ขŒ์šฐ๋กœ ์ด๋™ํ•˜์—ฌ ๋ฐฐ์—ด ๋ฒ”์œ„ ์•ˆ์ด๊ณ  ๊ฐ’์ด *์ด ์•„๋‹ˆ๋ฉฐ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์ด๋ผ๋ฉด ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ ๋ฐ queue์— ์ถ”๊ฐ€. ๋„์ฐฉ์œ„์น˜๋ผ๋ฉด temp[2]+1 ๋ฐ˜ํ™˜

3) ํ˜„์žฌ ๊ฐ’์ด -๋ผ๋ฉด ์ขŒ์šฐ๋กœ ์ด๋™ํ•˜์—ฌ ๋ฐฐ์—ด ๋ฒ”์œ„ ์•ˆ์ด๊ณ  ๊ฐ’์ด *์ด ์•„๋‹ˆ๋ฉฐ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์ด๋ผ๋ฉด ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ ๋ฐ queue์— ์ถ”๊ฐ€. ๋„์ฐฉ์œ„์น˜๋ผ๋ฉด temp[2]+1 ๋ฐ˜ํ™˜

4) ํ˜„์žฌ ๊ฐ’์ด |๋ผ๋ฉด ์ƒํ•˜๋กœ ์ด๋™ํ•˜์—ฌ ๋ฐฐ์—ด ๋ฒ”์œ„ ์•ˆ์ด๊ณ  ๊ฐ’์ด *์ด ์•„๋‹ˆ๋ฉฐ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์ด๋ผ๋ฉด ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ ๋ฐ queue์— ์ถ”๊ฐ€. ๋„์ฐฉ์œ„์น˜๋ผ๋ฉด temp[2]+1 ๋ฐ˜ํ™˜

๋„์ฐฉ ์œ„์น˜๊นŒ์ง€ ์ด๋™ํ•  ์ˆ˜ ์—†๋‹ค๋ฉด -1 ๋ฐ˜ํ™˜



 

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

[Baekjoon] 5462_POI  (0) 2024.11.26
[Baekjoon] 6191_Cows on Skates  (0) 2024.11.25
[Baekjoon] 14145_ลฝetva  (0) 2024.11.21
[Baekjoon] 6004_The Chivalrous Cow  (1) 2024.11.20
[Baekjoon] 6601_Knight Moves  (0) 2024.11.19