02
07

https://www.acmicpc.net/problem/1012

 

1012๋ฒˆ: ์œ ๊ธฐ๋† ๋ฐฐ์ถ”

์ฐจ์„ธ๋Œ€ ์˜๋†์ธ ํ•œ๋‚˜๋Š” ๊ฐ•์›๋„ ๊ณ ๋žญ์ง€์—์„œ ์œ ๊ธฐ๋† ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ๋†์•ฝ์„ ์“ฐ์ง€ ์•Š๊ณ  ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๋ ค๋ฉด ๋ฐฐ์ถ”๋ฅผ ํ•ด์ถฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ๋‚˜๋Š” ํ•ด์ถฉ ๋ฐฉ์ง€์— 

www.acmicpc.net

import java.util.*;

class Main {
	// https://www.acmicpc.net/problem/1012

	static int[] dx = { -1, 0, 1, 0 };
	static int[] dy = { 0, 1, 0, -1 }; // ์‚ฌ๋ฐฉ ๋Œ๊ธฐ
	static boolean[][] v;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int tc = sc.nextInt();
		StringBuilder sb = new StringBuilder();
		for (int t = 0; t < tc; t++) {
			int m = sc.nextInt();
			int n = sc.nextInt();
			int baechu = sc.nextInt();
			int[][] map = new int[m][n];
			v = new boolean[m][n];
			for (int i = 0; i < baechu; i++) {
				int x = sc.nextInt();
				int y = sc.nextInt();
				map[x][y] = 1;
			}
			// ์—ฌ๊ธฐ๊นŒ์ง€ ๋งต ์งœ๋Š” ๊ฑฐ
			int sum = 0;
			// bfs ์งœ๊ธฐ
			for (int x = 0; x < m; x++) {
				for (int y = 0; y < n; y++) {
					if (map[x][y] == 1 && !v[x][y]) {
						sum++;
						Queue<Integer> q = new LinkedList<>();
						q.offer(map[x][y]);
						v[x][y] = true;
						while (!q.isEmpty()) {
							q.poll();
							for (int i = 0; i < 4; i++) {
								int nx = dx[i] + x;
								int ny = dy[i] + y;
								if (!v[nx][ny] && nx < m && nx >= 0 && ny < n && ny >= 0) {
									q.offer(map[nx][ny]);
									v[nx][ny] = true;
								}
							}
						}
					}
				}
			}
			sb.append(sum).append("\n");
		}
		System.out.println(sb);

		sc.close();
	}
}

 

 


์˜ค๋‹ต๋…ธํŠธ(ํ‘ธ๋Š” ๋™์•ˆ ์‚ฌ๊ณ ๊ณผ์ • ๋ฐ ๊ฐœ์„ ์ )

์ ‘๊ทผ์€ ๋งž์•˜์œผ๋‚˜ BFS ๋Œ๋ฆด ๋•Œ๊ฐ€ ์ž˜๋ชป๋๋‹ค. ์ขŒํ‘œ๋ฅผ ๋ฐ›์•„์„œ ๊ทธ๊ฑธ ๊ธฐ์ ์œผ๋กœ bfs๋ฅผ ๋Œ๋ ค์•ผ ๋˜๋Š”๋ฐ ์ด๋ ‡๊ฒŒํ•˜๋ฉด ์›ํ•˜๋Š” ๋Œ€๋กœ ๋Œ์ง€ ์•Š๋Š”๋‹ค.

ํ™•์‹คํ•˜๊ฒŒ ๊ตฌ์—ญ์ด ๋‚˜๋ˆ ์ง„ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๊ฒƒ์ด๋ผ์„œ ์ „ํ˜•์ ์ธ bfs, dfs ๋ฌธ์ œ๋‹ค. ์•„๋งˆ dfs๋กœ ํ’€์—ˆ์œผ๋ฉด ํ•œ๋ฒˆ์— ํ’€๋ ธ์„ ๊ฒƒ์ด๋‹ค.


์ •๋‹ต์ฝ”๋“œ

import java.util.*;

class Main {
	// https://www.acmicpc.net/problem/1012

	static int[] dx = { -1, 0, 1, 0 };
	static int[] dy = { 0, 1, 0, -1 }; // ์‚ฌ๋ฐฉ ๋Œ๊ธฐ
	static int n;
	static int m;
	static int[][] map;
	static boolean[][] v;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int tc = sc.nextInt();
		StringBuilder sb = new StringBuilder();
		for (int t = 0; t < tc; t++) {
			m = sc.nextInt();
			n = sc.nextInt();
			int baechu = sc.nextInt();
			// nxm ์ด ์ž์—ฐ์Šค๋Ÿฝ๋‹ค.
			map = new int[n][m];
			v = new boolean[n][m];

			int sum = 0;

			for (int i = 0; i < baechu; i++) {
				int c = sc.nextInt();
				int r = sc.nextInt();
				map[r][c] = 1;
			}

			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					if (!v[i][j] && map[i][j] == 1) {
						sum++;
						bfs(i, j);
					}
				}
			}
			sb.append(sum).append("\n");
		}
		System.out.println(sb);

		sc.close();
	}

	public static void bfs(int x, int y) {
		Queue<int[]> q = new LinkedList<>();
		q.offer(new int[] { x, y });
		v[x][y] = true;

		while (!q.isEmpty()) {
			int[] curr = q.poll();
			int cx = curr[0];
			int cy = curr[1];

			for (int k = 0; k < 4; k++) {
				int nx = cx + dx[k];
				int ny = cy + dy[k];
				if (nx >= 0 && nx < n && ny >= 0 && ny < m && !v[nx][ny] && map[nx][ny] == 1) {
					q.offer(new int[] { nx, ny });
					v[nx][ny] = true;
				}
			}
		}
	}
}

 

์ „์ฒด ์ •๋‹ต ์ฝ”๋“œ๋‹ค. bfs๋งŒ ๋˜‘ ๋–ผ๊ฐ€์ง€๊ณ  ์ฒœ์ฒœํžˆ ๋ณด์ž.

public static void bfs(int x, int y) {
		Queue<int[]> q = new LinkedList<>();
		q.offer(new int[] { x, y });
		v[x][y] = true;

		while (!q.isEmpty()) {
			int[] curr = q.poll();
			int cx = curr[0];
			int cy = curr[1];

			for (int k = 0; k < 4; k++) {
				int nx = cx + dx[k];
				int ny = cy + dy[k];
				if (nx >= 0 && nx < n && ny >= 0 && ny < m && !v[nx][ny] && map[nx][ny] == 1) {
					q.offer(new int[] { nx, ny });
					v[nx][ny] = true;
				}
			}
		}
	}

Queue์˜ Typed์œผ๋กœ int๋ฐฐ์—ด์„ ๋„ฃ๊ณ , ํ ์‹œ์ž‘ํ•  ๋•Œ ํ˜„์žฌ ์œ„์น˜(1์ด ๊ฒ€์ถœ๋œ) ์ขŒํ‘œ๋ฅผ ํฌ๊ธฐ 2์งœ๋ฆฌ ์ดˆ๊ธฐํ™”๋œ int๋ฐฐ์—ด๋กœ ๋„ฃ์–ด์ค€๋‹ค.
์ด๊ฒŒ C++์ด๋‚˜ ์ฝ”ํ‹€๋ฆฐ์ด์—ˆ๋‹ค๋ฉด pair๋ฅผ ์‚ฌ์šฉํ–ˆ์œผ๋ฉด ๋  ํ…Œ์ง€๋งŒ ์ž๋ฐ”๋Š” ๊ทธ๋Ÿฐ๊ฒŒ ์—†๋‹ค. ํด๋ž˜์Šค ๋งŒ๋“ค์–ด์„œ ์“ฐ๋Š” ๋ฐฉ๋ฒ•์€ ์•„์ง ์ต์ˆ™ํ•˜์ง€์•Š์•„์„œ ํฌ๊ธฐ 2์งœ๋ฆฌ ๋ฐฐ์—ด๋กœ ๋Œ€์‹ ํ–ˆ๋‹ค.

 

์ด ๋ฐฐ์—ด๋กœ ์ขŒํ‘œ๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์„ ์ œ์™ธํ•˜๋ฉด ์›๋ž˜ ์ง  ์ฝ”๋“œ์—์„œ ๋‹ค๋ฅผ๊ฒŒ ์—†๋‹ค.

๋„์›€์ด ๋๋‹ค๋ฉด ๋Œ“๊ธ€์ด๋‚˜ ๊ณต๊ฐ ๋ฒ„ํŠผ ํ•œ ๋ฒˆ์”ฉ ๋ˆ„๋ฅด๊ณ  ๊ฐ€์ฃผ์„ธ์š”! ๋กœ๊ทธ์ธ ์•ˆํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค ^_^

 

๋ฐ˜์‘ํ˜•
COMMENT