Repository navigation
Expand file tree
/
Copy pathMain_10026_BFS.java
More file actions
111 lines (94 loc) · 2.61 KB
/
Copy pathMain_10026_BFS.java
File metadata and controls
111 lines (94 loc) · 2.61 KB
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
package baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/*
* 적록색약
* */
public class Main_10026_BFS {
static int n;
static int blind; // 적록색약
static int eye; // 적록색약 아닌 사람
static char[][] eyeBoard;
static char[][] blindeyeBoard;
static boolean[][] blindVisited;
static boolean[][] eyeVisited;
static int[] dx = {0, 0, -1, 1};
static int[] dy = {-1, 1, 0, 0};
static class Pointer {
int x;
int y;
public Pointer(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void bfs(Pointer pointer) {
Queue<Pointer> queue = new LinkedList<>();
queue.offer(pointer);
eyeVisited[pointer.x][pointer.y] = true;
while (!queue.isEmpty()) {
Pointer current = queue.poll();
for (int i = 0; i < dx.length; i++) {
int nx = current.x + dx[i];
int ny = current.y + dy[i];
if (nx >= 0 && ny >= 0 && nx < n && ny < n && eyeVisited[nx][ny] == false && eyeBoard[nx][ny] == eyeBoard[current.x][current.y]) {
queue.offer(new Pointer(nx, ny));
eyeVisited[nx][ny] = true;
}
}
}
}
public static void bfs2(Pointer pointer) {
Queue<Pointer> queue = new LinkedList<>();
queue.offer(pointer);
blindVisited[pointer.x][pointer.y] = true;
while (!queue.isEmpty()) {
Pointer current = queue.poll();
for (int i = 0; i < dx.length; i++) {
int nx = current.x + dx[i];
int ny = current.y + dy[i];
if (nx >= 0 && ny >= 0 && nx < n && ny < n && blindVisited[nx][ny] == false && blindeyeBoard[nx][ny] == blindeyeBoard[current.x][current.y]) {
queue.offer(new Pointer(nx, ny));
blindVisited[nx][ny] = true;
}
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
blindVisited = new boolean[n][n];
eyeVisited = new boolean[n][n];
eyeBoard = new char[n][n];
blindeyeBoard = new char[n][n];
for (int i = 0; i < n; i++) {
char[] arr = br.readLine().toCharArray();
for (int j = 0; j < n; j++) {
eyeBoard[i][j] = arr[j];
blindeyeBoard[i][j] = arr[j];
if (arr[j] == 'G') {
blindeyeBoard[i][j] = 'R';
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (eyeVisited[i][j] == false) {
Main_10026_BFS.bfs(new Pointer(i, j));
eye++;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (blindVisited[i][j] == false) {
Main_10026_BFS.bfs2(new Pointer(i, j));
blind++;
}
}
}
System.out.println(eye + " " + blind);
}
}