-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBAEKJOON_10026
More file actions
112 lines (101 loc) · 2.92 KB
/
BAEKJOON_10026
File metadata and controls
112 lines (101 loc) · 2.92 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
112
import java.io.*;
import java.sql.Array;
import java.util.*;
public class Main {
static class Location{
int x;
int y;
public Location(int x, int y) {
this.x = x;
this.y = y;
}
}
static int N;
static char[][] board;
static boolean[][] visit;
static int[] dx = new int[]{0, 1, 0, -1};
static int[] dy = new int[]{1, 0, -1, 0};
static int count=0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
N = Integer.parseInt(br.readLine());
board = new char[N][N];
visit = new boolean[N][N];
StringBuilder sb;
for(int i=0; i<N; i++){
sb = new StringBuilder(br.readLine());
for(int j=0; j<N; j++){
board[i][j] = sb.toString().charAt(j);
}
}
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(visit[i][j]==false){
count++;
DFS_1(i,j,board[i][j]);
}
}
}
bw.write(String.valueOf(count));
convert_board(board);
init_visit(visit);
count =0;
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(visit[i][j]==false){
count++;
DFS_2(i,j,board[i][j]);
}
}
}
bw.write(String.valueOf(" "+count));
bw.newLine();
bw.flush();
bw.close();
}
public static void DFS_1(int x,int y,char color){
if(visit[x][y]){
return;
}
visit[x][y] =true;
for(int i=0; i<4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < N && ny < N && nx >= 0 && ny >= 0 && board[nx][ny]==color) {
if (!visit[nx][ny]) {
DFS_1(nx, ny,board[nx][ny]);
}
}
}
}
public static void DFS_2(int x, int y, char color) {
if (visit[x][y]) {
return;
}
visit[x][y] =true;
for(int i=0; i<4; i++){
int nx = x +dx[i];
int ny = y +dy[i];
if (nx < N && ny < N && nx >= 0 && ny >= 0 && board[nx][ny] == color) {
if (!visit[nx][ny]) {
DFS_2(nx, ny, board[nx][ny]);
}
}
}
}
public static void init_visit(boolean[][] visit){
for(int i=0; i<N; i++){
Arrays.fill(visit[i], false);
}
}
public static void convert_board(char[][] board){
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if (board[i][j] == 'G') {
board[i][j] = 'R';
}
}
}
}
}