๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2๏ธโƒฃ Java/Problem Solving

[Java Algorithm] ์ ๋ก์ƒ‰์•ฝ BOJ #10026

by seolhee2750 2022. 8. 31.
๋ฌธ์ œ

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

 

10026๋ฒˆ: ์ ๋ก์ƒ‰์•ฝ

์ ๋ก์ƒ‰์•ฝ์€ ๋นจ๊ฐ„์ƒ‰๊ณผ ์ดˆ๋ก์ƒ‰์˜ ์ฐจ์ด๋ฅผ ๊ฑฐ์˜ ๋Š๋ผ์ง€ ๋ชปํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์ด ๋ณด๋Š” ๊ทธ๋ฆผ์€ ์•„๋‹Œ ์‚ฌ๋žŒ์ด ๋ณด๋Š” ๊ทธ๋ฆผ๊ณผ๋Š” ์ข€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ํฌ๊ธฐ๊ฐ€ N×N์ธ ๊ทธ๋ฆฌ๋“œ์˜ ๊ฐ ์นธ์— R(๋นจ๊ฐ•), G(์ดˆ๋ก)

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class BOJ_10026 {

	static int n, cnt;
	static char[][] map, tmp;
	static Deque<int[]> que = new ArrayDeque<>();

	public static void dfs(int x, int y, char color) {
		if(x < 0 || y < 0 || x >= n || y >= n) return;
		if(color != tmp[x][y]) return;

		tmp[x][y] = 'F'; // ์ค‘๋ณต ๋ฐฉ์ง€

		dfs(x, y-1, color);
		dfs(x, y+1, color);
		dfs(x-1, y, color);
		dfs(x+1, y, color);
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder out = new StringBuilder();

		n = Integer.parseInt(in.readLine());
		map = new char[n][n];

		for(int i = 0; i < n; i++) {
			String str = in.readLine();
			for(int j = 0; j < n; j++) {
				map[i][j] = str.charAt(j);
			}
		}

		tmp = new char[n][n];
		for(int i = 0; i < n; i++) tmp[i] = Arrays.copyOf(map[i], n);
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				if(tmp[i][j] != 'F') {
					dfs(i, j, tmp[i][j]);
					cnt++;
				}
			}
		}
		out.append(cnt + " ");
		cnt = 0;

		for(int i = 0; i < n; i++) tmp[i] = Arrays.copyOf(map[i], n);
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				if(tmp[i][j] == 'G') tmp[i][j] = 'R';
			}
		}
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				if(tmp[i][j] != 'F') {
					dfs(i, j, tmp[i][j]);
					cnt++;
				}
			}
		}
		out.append(cnt);

		System.out.println(out);
	}

}

๐Ÿ‘‰ DFS ๋ฌธ์ œ

์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ์‚ฌ๋žŒ์˜ ๊ธฐ์ค€์œผ๋กœ ๋จผ์ € ํ™•์ธ ํ›„ ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์˜ ๊ธฐ์ค€์œผ๋กœ ํ™•์ธํ•˜๋Š” ์‹์œผ๋กœ ๊ตฌํ˜„

  • map ๋ฐฐ์—ด์„ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ tmp ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ๋ณต์‚ฌํ•ด์„œ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ๋จผ์ € ์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ์‚ฌ๋žŒ์˜ ๊ธฐ์ค€์œผ๋กœ ํ™•์ธํ–ˆ๋Š”๋ฐ,
    tmp ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•˜๋ฉด์„œ 'F'๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ dfs ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ํ–ˆ๋‹ค.
  • dfs ๋ฉ”์„œ๋“œ์—์„œ๋Š” ํƒ์ƒ‰ํ•˜๋ฉด์„œ, ์ธ์ ‘ํ•˜๋ฉฐ ๊ฐ™์€ ๋ฌธ์ž๋ฅผ ๊ฐ€์ง„ ๊ณณ๋“ค์„ ์ฐพ๊ณ 
    ๊ทธ ๊ณณ๋“ค์€ ๋ชจ๋‘ 'F'๋กœ ๋ฐ”๊ฟ”์ฃผ์–ด ์ค‘๋ณต๋œ ํƒ์ƒ‰์„ ํ•  ์ˆ˜ ์—†๋„๋ก ํ•ด์ฃผ์—ˆ๋‹ค.
  • ์œ„์™€ ๊ฐ™์€ ์‹คํ–‰์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ๊ทธ๋ฃน์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ณ , out์— cnt๋ฅผ ์ถ”๊ฐ€ํ•œ ๋’ค cnt๋Š” ๋‹ค์‹œ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ–ˆ๋‹ค.
  • ์ด์ œ ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์˜ ๊ธฐ์ค€์œผ๋กœ ํ™•์ธํ•  ์ฐจ๋ก€์ด๋ฏ€๋กœ, ๋‹ค์‹œ tmp์— map ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•ด์ค€ ๋’ค,
    tmp๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ 'G'์ธ ์ž๋ฆฌ๋Š” 'R'๋กœ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋‹ค. (๊ฐ™์€ ์ƒ‰์œผ๋กœ ์ธ์ง€ํ•˜๋ฏ€๋กœ)
  • ๊ทธ ๋‹ค์Œ ์œ„์—์„œ ์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ์‚ฌ๋žŒ์˜ ๊ธฐ์ค€์œผ๋กœ ํ™•์ธํ–ˆ๋˜ ๊ฒƒ๊ณผ ๊ฐ™์€ ๋™์ž‘์œผ๋กœ ํƒ์ƒ‰์„ ์‹คํ–‰ํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€๋‹ค.!! ๊ทผ๋ฐ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ์–ด์ฐจํ”ผ 'F'๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ dfs๋ฅผ ์‹คํ–‰ํ•˜๋Š” 2์ค‘ for๋ฌธ์€
    ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋‘ ๋ฒˆ ๋ฐ˜๋ณตํ•˜๋‹ˆ,, ๋ฉ”์„œ๋“œ๋กœ ๋งŒ๋“ค์–ด์„œ ์ฒ˜๋ฆฌํ•ด์คฌ์œผ๋ฉด ๋” ๊น”๋”ํ–ˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

๋Œ“๊ธ€