-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
71 lines (57 loc) · 1.88 KB
/
Main.java
File metadata and controls
71 lines (57 loc) · 1.88 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N;
static int M;
static int[][] visitied;
static int[] dc = {-1, 1, 0, 0};
static int[] dr = {0, 0, 1, -1};
static char[][] field;
static int count=0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
field = new char[M][N];
visitied = new int[M][N];
int White=0;
int Bule=0;
for (int i = 0; i < M; i++) {
String s = br.readLine();
for (int j = 0; j < N; j++) {
field[i][j] = s.charAt(j);
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (visitied[i][j] == 0) {
visitied[i][j] = 1;
count = 1;
DFS(i, j, field[i][j]);
if(field[i][j]=='W'){
White+=Math.pow(count,2);
}else{
Bule+=Math.pow(count,2);
}
}
}
}
System.out.println(White+" "+Bule);
}
private static void DFS(int col, int row, char pattern) {
for (int i = 0; i < 4; i++) {
int cc = col + dc[i];
int cr = row + dr[i];
if (0 <= cc && cc < M && 0 <= cr && cr < N) {
if (visitied[cc][cr] == 0 && field[cc][cr] == pattern){
visitied[cc][cr] = 1;
count++;
DFS(cc, cr, pattern);
}
}
}
}
}