[BaekJoon] 10026번 - 적록색약 [Java][C++]
[BaekJoon] 10026번 - 적록색약 [Java][C++]
1. 아이디어
적록색약이 아닌 사람은 RGB를 모두 구분하고 적록색약인 사람은 R과 G를 동일하게 볼 때 구역의 수를 구하는 문제로 적록색약이 아닌 사람이면 두 색상이 동일한지를 반환하고, 적록색약인 사람이면 두 색상 중 하나가 B일 때는 두 색상이 동일한지, 아니면 true를 반환하는 isSame 함수를 탐색 조건으로 활용했다.
2. 코드
1. BFS [Java]
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
import java.io.*;
import java.util.*;
public class Main {
static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, 1, 0, -1};
static int n;
static char[][] grid;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
n = Integer.parseInt(br.readLine());
grid = new char[n][n];
for (int i = 0; i < n; i++) {
grid[i] = br.readLine().toCharArray();
}
boolean isBlind = false;
for (int tc = 1; tc <= 2; tc++) {
boolean[][] vis = new boolean[n][n];
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (vis[i][j]) continue;
bfs(i, j, vis, isBlind);
cnt++;
}
}
sb.append(cnt).append(" ");
isBlind = !isBlind;
}
System.out.println(sb);
}
static void bfs(int sr, int sc, boolean[][] vis, boolean isBlind) {
Queue<int[]> q = new ArrayDeque<>();
q.offer(new int[]{sr, sc});
vis[sr][sc] = true;
while (!q.isEmpty()) {
int[] cur = q.poll();
for (int d = 0; d < 4; d++) {
int nr = cur[0] + dr[d];
int nc = cur[1] + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
if (!isSame(grid[sr][sc], grid[nr][nc], isBlind) || vis[nr][nc]) continue;
q.offer(new int[]{nr, nc});
vis[nr][nc] = true;
}
}
}
static boolean isSame(char c1, char c2, boolean isBlind) {
if (!isBlind) return c1 == c2;
if (c1 == 'B' || c2 == 'B') return c1 == c2;
return true;
}
}
2. BFS [C++]
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
#include <bits/stdc++.h>
using namespace std;
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
int n;
char grid[101][101];
bool vis[101][101];
bool isSame(char c1, char c2, bool isBlind) {
if (!isBlind) return c1 == c2;
if (c1 == 'B' || c2 == 'B') return c1 == c2;
return true;
}
void bfs(int sr, int sc, bool isBlind) {
queue<pair<int, int>> q;
q.push({sr, sc});
vis[sr][sc] = true;
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
for (int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
if (!isSame(grid[sr][sc], grid[nr][nc], isBlind) || vis[nr][nc]) continue;
q.push({nr, nc});
vis[nr][nc] = true;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> grid[i];
}
bool isBlind = false;
for (int tc = 1; tc <= 2; tc++) {
memset(vis, 0, sizeof(vis));
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (vis[i][j]) continue;
bfs(i, j, isBlind);
cnt++;
}
}
cout << cnt << ' ';
isBlind = !isBlind;
}
}
3. DFS [Java]
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
import java.io.*;
public class Main {
static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, 1, 0, -1};
static int n;
static char[][] grid;
static boolean[][] vis;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
n = Integer.parseInt(br.readLine());
grid = new char[n][n];
for (int i = 0; i < n; i++) {
grid[i] = br.readLine().toCharArray();
}
boolean isBlind = false;
for (int tc = 1; tc <= 2; tc++) {
vis = new boolean[n][n];
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (vis[i][j]) continue;
dfs(i, j, isBlind);
cnt++;
}
}
sb.append(cnt).append(" ");
isBlind = !isBlind;
}
System.out.println(sb);
}
static void dfs(int r, int c, boolean isBlind) {
vis[r][c] = true;
for (int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
if (!isSame(grid[r][c], grid[nr][nc], isBlind) || vis[nr][nc]) continue;
dfs(nr, nc, isBlind);
}
}
static boolean isSame(char c1, char c2, boolean isBlind) {
if (!isBlind) return c1 == c2;
if (c1 == 'B' || c2 == 'B') return c1 == c2;
return true;
}
}
4. DFS [C++]
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
#include <bits/stdc++.h>
using namespace std;
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
int n;
char grid[101][101];
bool vis[101][101];
bool isSame(char c1, char c2, bool isBlind) {
if (!isBlind) return c1 == c2;
if (c1 == 'B' || c2 == 'B') return c1 == c2;
return true;
}
void dfs(int r, int c, bool isBlind) {
vis[r][c] = true;
for (int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
if (!isSame(grid[r][c], grid[nr][nc], isBlind) || vis[nr][nc]) continue;
dfs(nr, nc, isBlind);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> grid[i];
}
bool isBlind = false;
for (int tc = 1; tc <= 2; tc++) {
memset(vis, 0, sizeof(vis));
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (vis[i][j]) continue;
dfs(i, j, isBlind);
cnt++;
}
}
cout << cnt << ' ';
isBlind = !isBlind;
}
}
3. 디버깅
없음.
4. 참고
없음.
This post is licensed under CC BY 4.0 by the author.