Home G10026. 적록색약
Post
Cancel

G10026. 적록색약

문제

image


제출 코드

image


  • 사용 알고리즘 : DFS


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package gold;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class G10026_redGreenColorWeekness {

	static int N;
	static char[][] color;
	static int[] di = {-1, 0, 1, 0};
	static int[] dj = {0, 1, 0, -1};

	static void dfs(int i, int j, char old, char nw) {
		color[i][j] = nw;
		for(int d=0; d<4; d++) {
			int ni = i + di[d];
			int nj = j + dj[d];
			if(ni>=0 && ni<N && nj>=0 && nj<N && color[ni][nj]==old) dfs(ni, nj, old, nw);
		}
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine());

		color = new char[N][N];
		for(int n=0; n<N; n++) {
			String s = br.readLine();
			color[n] = s.toCharArray();
		}
		int cnt = 0;
		for(int i=0; i<N; i++) {
			for(int j=0; j<N; j++) {
				if(color[i][j]=='R' || color[i][j]=='G') {
					dfs(i, j, color[i][j], 'X');
					cnt++;
				}else if(color[i][j]=='B'){
					dfs(i, j, 'B', 'Y');
					cnt++;
				}
			}
		}
		System.out.print(cnt + " ");

		cnt = 0;
		for(int i=0; i<N; i++) {
			for(int j=0; j<N; j++) {
				if(color[i][j]=='X') {
					dfs(i, j, 'X', '-');
					cnt++;
				}else if(color[i][j]=='Y'){
					dfs(i, j, 'Y', '-');
					cnt++;
				}
			}
		}
		System.out.println(cnt);
	}
}
This post is licensed under CC BY 4.0 by the author.