๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 18404_ํ˜„๋ช…ํ•œ ๋‚˜์ดํŠธ

๋ฟŒ์•ผ._. 2023. 7. 28. 14:14

Silver I

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

< ํ˜„๋ช…ํ•œ ๋‚˜์ดํŠธ >

 

๋ฌธ์ œ ํ’€์ด 

 

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;
import java.util.StringTokenizer;

public class _18404_ { // ํ˜„๋ช…ํ•œ ๋‚˜์ดํŠธ
	static boolean visited[][];
	static int arr[][], result[], n, cnt;
	static int dx[] = { -2, -2, -1, -1, 1, 1, 2, 2 };
	static int dy[] = { -1, 1, -2, 2, -2, 2, -1, 1 };

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

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

		visited = new boolean[n][n];
		cnt = 0;

		st = new StringTokenizer(bf.readLine());
		int x = Integer.parseInt(st.nextToken()) - 1;
		int y = Integer.parseInt(st.nextToken()) - 1;

		arr = new int[m][2];
		result = new int[m];
		for (int i = 0; i < m; i++) {
			st = new StringTokenizer(bf.readLine());
			int a = Integer.parseInt(st.nextToken()) - 1;
			int b = Integer.parseInt(st.nextToken()) - 1;
			arr[i][0] = a;
			arr[i][1] = b;
		}

		bfs(x, y);

		for (int i = 0; i < m; i++) {
			bw.write(result[i] + " ");
		}
		bw.flush();
	}

	private static void bfs(int x, int y) {
		Queue<int[]> queue = new LinkedList<>();
		queue.add(new int[] { x, y, 0 });
		visited[x][y] = true;

		while (!queue.isEmpty()) {
			int temp[] = queue.poll();
			for (int i = 0; i < 8; i++) {
				int a = temp[0] + dx[i];
				int b = temp[1] + dy[i];
				if (a >= 0 && a < n && b >= 0 && b < n && !visited[a][b]) {
					visited[a][b] = true;
					for (int j = 0; j < arr.length; j++) {
						if (a == arr[j][0] && b == arr[j][1]) {
							result[j] = temp[2] + 1;
							cnt += 1;
							break;
						}
					}
					queue.add(new int[] { a, b, temp[2] + 1 });
				}
				if (cnt == result.length)
					break;
			}
			if (cnt == result.length)
				break;
		}
	}
}

 

Main

๋ณ€์ˆ˜)
n : ์ฒด์ŠคํŒ ํฌ๊ธฐ
m : ์ƒ๋Œ€ํŽธ ๋ง์˜ ๊ฐœ์ˆ˜
visited : ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
cnt : ์ƒ๋Œ€ํŽธ ๋ง ์žก์€ ์ˆ˜
x, y : ๋‚˜์ดํŠธ์˜ ์œ„์น˜
arr : ์ƒ๋Œ€ํŽธ ๋ง ์œ„์น˜ ์ €์žฅ
result : ์ƒ๋Œ€ํŽธ ๋ง์„ ์žก๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ์ด๋™ ์ˆ˜ ์ €์žฅ
a, b : ์ƒ๋Œ€ํŽธ ๋ง์˜ ์œ„์น˜

 

- ์ฒด์ŠคํŒ์˜ ํฌ๊ธฐ(n), ์ƒ๋Œ€ํŽธ ๋ง์˜ ๊ฐœ์ˆ˜(m) ์ž…๋ ฅ

- ๋‚˜์ดํŠธ์˜ ์œ„์น˜ (x, y) ์ž…๋ ฅ

- m๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ์ƒ๋Œ€ํŽธ์˜ ๋ง์˜ ์œ„์น˜(a, b)๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ €์žฅ

- bfs ํ•จ์ˆ˜ ํ˜ธ์ถœ

- ์ •๋‹ต(result) ์ถœ๋ ฅ

 

bfs

- queue์— ์ €์žฅ [x, y, ์ด๋™ ์ˆ˜]

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

: 8๊ฐ€์ง€ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•˜์—ฌ ์ฒด์ŠคํŒ ๋ฒ”์œ„ ์•ˆ์ด๋ฉฐ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋ฐฉ๋ฌธ ํ‘œ์‹œ ๋ฐ queue์— ์ €์žฅ

: ์ด๋™ํ•œ ์œ„์น˜๊ฐ€ ์ƒ๋Œ€ํŽธ ๋ง์ด ์žˆ๋Š” ์œ„์น˜๋ผ๋ฉด ์ตœ์†Œ ์ด๋™ ์ˆ˜ ์ €์žฅ