๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

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

category Algorithm/BOJ 2022. 8. 24. 22:26

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

 

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

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

www.acmicpc.net


1. ๋ฌธ์ œ ์š”์•ฝ

์ฃผ์–ด์ง„ map์— ๋Œ€ํ•˜์—ฌ, ์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ์‚ฌ๋žŒ์€ R,G,B๋ฅผ ๊ฐ๊ฐ ๋‹ค๋ฅธ์ƒ‰์œผ๋กœ ๋ณด๊ณ  ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์€ R,G๋Š” ๊ฐ™์€ ์ƒ‰์œผ๋กœ ๋ณธ๋‹ค.

์ด ๋•Œ, ๊ฐ™์€ ์ƒ‰๋ผ๋ฆฌ์˜ ๊ตฌ์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

 

2. ํ’€์ด

DFS๋กœ ์ˆœํšŒํ•œ ๊ตฌ์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์ฃผ๋Š” ๋ฌธ์ œ์ด๋‹ค. DFS ํ•จ์ˆ˜๋Š” ์ „ํ˜•์ ์ธ ํ‹€์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

์ฃผ์˜ํ•  ์ ์€ ์ ๋ก์ƒ‰์•ฝ์ธ ๊ฒฝ์šฐ continue ์กฐ๊ฑด์ด๋‹ค.

๋‚˜๋Š” ์ด ์กฐ๊ฑด์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฃผ์—ˆ๋‹ค.

์‹œ์ž‘์ ์ด R๋˜๋Š” G์ธ ๊ฒฝ์šฐ, B๋ฅผ ๋งŒ๋‚˜๋ฉด ๋„˜์–ด๊ฐ€๊ฒ ๋‹ค (R,G๋ฅผ ๊ฐ™์€ ์ƒ‰์œผ๋กœ ๋ฌถ์Œ)

์‹œ์ž‘์ ์ด B์ธ ๊ฒฝ์šฐ, B๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ์ƒ‰์„ ๋งŒ๋‚˜๋ฉด ๋„˜์–ด๊ฐ€๊ฒ ๋‹ค

 

1. map์—์„œ ๋ชจ๋“  ์ ์„ ์ˆœํšŒํ•˜๋ฉฐ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ ์— ๋Œ€ํ•ด์„œ DFS๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

2. DFS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ™์€ ์ƒ‰๋ผ๋ฆฌ์˜ ๊ตฌ์—ญ์„ ๋ชจ๋‘ ๋ฐฉ๋ฌธํ•œ๋‹ค, ๋ฐฉ๋ฌธํ–ˆ์œผ๋ฉด visited๋ฅผ true๋กœ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค.

3. ์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ๊ฒฝ์šฐ dfs1(), visited1 / ์ ๋ก์ƒ‰์•ฝ์ธ ๊ฒฝ์šฐ dfs2(), visited2๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

3. ๋ฌธ์ œ์ 

1. ์ค‘๋ณต๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ๋งŽ๋‹ค.

- dfs ํ•จ์ˆ˜์˜ ํ‹€์€ ๊ฑฐ์˜ ๊ฐ™๊ณ , ์กฐ๊ฑด ํ•œ ๋‘์ค„๋งŒ ๋‹ค๋ฅด๋‹ค. 

 

2. ๋ฐฐ์—ด์ด ๋งŽ๋‹ค.

- ์ด์ค‘for๋ฌธ ๋Œ๋ฆด ๋•Œ ํ•œ๋ฒˆ์— 2๊ฐœ์˜ dfs๋ฅผ ๋Œ๋ฆฌ๋ ค๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— dfsํ•จ์ˆ˜ 2๊ฐœ, visited ๋ฐฐ์—ด 2๊ฐœ๊ฐ€ ํ•„์š”ํ–ˆ๋‹ค. 
๋ฐ˜๋ณต๋ฌธ์„ ์—ฌ๋Ÿฌ๋ฒˆ ๋Œ๋ฆฌ๋Š” ๊ฒƒ๋ณด๋‹ค ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ๋” ๋งŒ๋“œ๋Š”๊ฒŒ ๋‚ซ์ง€์•Š์„๊นŒ? ํ•˜๋Š” ์ƒ๊ฐ์œผ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.

 

4. ์ฝ”๋“œ

๋ฐฉ๋ฒ•1. dfs1(), dfs2() ๊ตฌ๋ถ„

๋”๋ณด๊ธฐ
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

	static int[] dy = {-1, 1, 0, 0};
	static int[] dx = {0, 0, -1, 1};
	static int N, cnt1, cnt2;
	static char color;
	static char[][] map;
	static boolean[][] visited1, visited2;
	
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine());
		map = new char[N][N];
		visited1 = new boolean[N][N];
		visited2 = new boolean[N][N];
		for (int i = 0; i < N; i++) {
			map[i] = br.readLine().toCharArray();
		}
		
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				color = map[i][j];
				if (!visited1[i][j]) {
					dfs1(i, j);
					cnt1++;
				}
				if (!visited2[i][j]) {
					dfs2(i, j);
					cnt2++;
				}
			}
		}
		System.out.println(cnt1 + " " + cnt2);
	}
	
	//์ ๋ก์ƒ‰์•ฝ ์•„๋‹˜
	static void dfs1(int y, int x) {
		visited1[y][x] = true;
		for (int d = 0; d < 4; d++) {
			int ny = y + dy[d];
			int nx = x + dx[d];
			if (ny < 0 || nx < 0 || ny >= N || nx >= N||visited1[ny][nx]) continue;
			if (map[ny][nx] != color) continue;
			visited1[ny][nx] = true;
			dfs1(ny, nx);
		}
		
	}

	// ์ ๋ก์ƒ‰์•ฝ
	static void dfs2(int y, int x) {
		visited2[y][x] = true;
		for (int d = 0; d < 4; d++) {
			int ny = y + dy[d];
			int nx = x + dx[d];
			if (ny < 0 || nx < 0 || ny >= N || nx >= N || visited2[ny][nx]) continue;
			if ((color == 'R' || color == 'G') && map[ny][nx] == 'B') continue;
			else if (color == 'B' && map[ny][nx] != 'B') continue;
			visited2[ny][nx] = true;
			dfs2(ny, nx);
		}
	}
}

๋ฐฉ๋ฒ•2. dfs ๊ฐ™์ด ์”€

* visited ๋ฐฐ์—ด์„ ์žฌํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ค‘๊ฐ„์— ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์ค˜์•ผ ํ–ˆ๋‹ค.

๋”๋ณด๊ธฐ
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class BOJ_10026_์ ๋ก์ƒ‰์•ฝ {

	static int[] dy = {-1, 1, 0, 0};
	static int[] dx = {0, 0, -1, 1};
	static int N, cnt1, cnt2;
	static char color;
	static char[][] map;
	static boolean[][] visited;
	
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine());
		map = new char[N][N];
		visited = new boolean[N][N];
		
		//์ž…๋ ฅ๋ฐ›๋Š”๋ฐฉ๋ฒ•1. ํ•œ์ค„์„ ํ†ต์งธ๋กœ ๋„ฃ์–ด์คŒ
		for (int i = 0; i < N; i++) {
			map[i] = br.readLine().toCharArray();
		}
		//์ž…๋ ฅ๋ฐ›๋Š”๋ฐฉ๋ฒ•2. ๋ฌธ์ž ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ๋„ฃ์–ด์คŒ -> ์ž…๋ ฅ๋ฐ›์„ ๋•Œ ์กฐ๊ฑด์„ ์ฒ˜๋ฆฌํ•ด์ค„ ์ˆ˜ ์žˆ์Œ
		/*
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				map[i][j] = br.readLine().charAt(j);
			}
		}
		*/
		
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				color = map[i][j];
				if (!visited[i][j]) {
					dfs1(i, j);
					cnt1++;
				}
			}
		}
		//visited ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•1 - Arrays.fill(๋ฐฐ์—ด, false);
		for (int i = 0; i < N; i++) {
			Arrays.fill(visited[i], false); 
		}
		//visited ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•2 - ๊ฐ์ฒด ์ƒˆ๋กœ ์ƒ์„ฑ
		//visited = new boolean[N][N];
		
		//map์˜ R->G๋กœ ๋ฐ”๊ฟ”์คŒ
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (map[i][j] == 'R') {
					map[i][j] = 'G';
				}
			}
		}
		
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				color = map[i][j];
				if (!visited[i][j]) {
					dfs1(i, j);
					cnt2++;
				}
			}
		}
		System.out.println(cnt1 + " " + cnt2);
	}
	
	//์ ๋ก์ƒ‰์•ฝ ์•„๋‹˜
	static void dfs1(int y, int x) {
		visited[y][x] = true;
		for (int d = 0; d < 4; d++) {
			int ny = y + dy[d];
			int nx = x + dx[d];
			if (ny < 0 || nx < 0 || ny >= N || nx >= N||visited[ny][nx]) continue;
			if (map[ny][nx] != color) continue;
			visited[ny][nx] = true;
			dfs1(ny, nx);
		}
		
	}

	/*
	// ์ ๋ก์ƒ‰์•ฝ
	static void dfs2(int y, int x) {
		visited2[y][x] = true;
		for (int d = 0; d < 4; d++) {
			int ny = y + dy[d];
			int nx = x + dx[d];
			if (ny < 0 || nx < 0 || ny >= N || nx >= N || visited2[ny][nx]) continue;
			if ((color == 'R' || color == 'G') && map[ny][nx] == 'B') continue;
			else if (color == 'B' && map[ny][nx] != 'B') continue;
			visited2[ny][nx] = true;
			dfs2(ny, nx);
		}
	}
	*/
}