최's 먹공로그
백준10026_적록색약 본문
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 |
package backjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class b10026_적록색약_최성호 {
static char[][] map;
static int[][] visited;
static int[][] visited2;
static int cnt;
static int cnt2;
static int n;
static int[] xs = {-1, 0, 1, 0};
static int[] ys = {0, 1, 0, -1};
// 적록색약이 있는 사람일 때 DFS
public static void dfs2(int x, int y) {
visited2[x][y] = 1;
for(int i = 0; i < 4; i++) {
int nx = x + xs[i];
int ny = y + ys[i];
if(nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
if((map[x][y] == 'R' || map[x][y] == 'G') && (map[nx][ny] == 'R' || map[nx][ny] == 'G') && visited2[nx][ny] == 0) {
dfs2(nx , ny);
}
else if(map[x][y] == 'B' && map[nx][ny] == 'B' && visited2[nx][ny] == 0) {
dfs2(nx , ny);
}
}
}
// 적록색약이 없는 사람일 때 DFS
public static void dfs(int x, int y) {
visited[x][y] = 1;
for(int i = 0; i < 4; i++) {
int nx = x + xs[i];
int ny = y + ys[i];
if(nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
if(map[x][y] == 'R' && map[nx][ny] == 'R' && visited[nx][ny] == 0) {
dfs(nx , ny);
}
else if(map[x][y] == 'B' && map[nx][ny] == 'B' && visited[nx][ny] == 0) {
dfs(nx , ny);
}
else if(map[x][y] == 'G' && map[nx][ny] == 'G' && visited[nx][ny] == 0) {
dfs(nx , ny);
}
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
map = new char[n][n];
visited = new int[n][n];
visited2 = new int[n][n];
for(int i = 0; i < n; i++) {
String str = br.readLine();
for (int j = 0; j < n; j++) {
map[i][j] = str.charAt(j);
}
}
// 적록색약이 없는 사람일 때
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(map[i][j] == 'R' && visited[i][j] == 0) { // map값이 R이고 visited 안했으면
dfs(i , j);
cnt++; // DFS끝 났다는 건 인접한 곳이 R인 부분 검사 끝났다는 뜻이므로 cnt++
}
else if(map[i][j] == 'B' && visited[i][j] == 0) {
dfs(i , j);
cnt++;
}
else if(map[i][j] == 'G' && visited[i][j] == 0) {
dfs(i , j);
cnt++;
}
}
}
// 적록색약이 있는 사람일 때
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if((map[i][j] == 'R' || map[i][j] == 'G') && visited2[i][j] == 0) { // map값에서 R=G로 봄
dfs2(i, j);
cnt2++;
}
else if(map[i][j] == 'B' && visited2[i][j] == 0) {
dfs2(i, j);
cnt2++;
}
}
}
System.out.println(cnt);
System.out.println(cnt2);
cnt = 0;
cnt2 = 0;
} // end of main
}
|
cs |
'APS' 카테고리의 다른 글
SW expert Academy 1208_[S/W 문제해결 기본]1일차_Flatten (0) | 2019.02.03 |
---|---|
SW expert Academy 5431_민석이의 과제 체크하기 (0) | 2019.02.03 |
백준2667_단지번호붙이기 (0) | 2019.02.03 |
백준1182_부분집합의 합 (0) | 2019.02.03 |
백준1018_체스판 다시 칠하기 (0) | 2019.02.03 |