-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp3184.java
More file actions
88 lines (83 loc) · 2.41 KB
/
p3184.java
File metadata and controls
88 lines (83 loc) · 2.41 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
/*
양
BFS 구현
*/
import java.io.*;
import java.util.*;
public class p3184 {
static class Pair {
int r, c;
public Pair(int r, int c) {
this.r = r;
this.c = c;
}
}
static int rr, cc, oo, vv;
static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, -1, 0, 1};
static boolean[][] visited;
static char[][] map;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
rr = Integer.parseInt(st.nextToken());
cc = Integer.parseInt(st.nextToken());
vv = 0;
oo = 0;
map = new char[rr][cc];
visited = new boolean[rr][cc];
for (int i = 0; i < rr; i++) {
char[] input = br.readLine().toCharArray();
for (int j = 0; j < cc; j++) {
char c = input[j];
if (c == 'v') {
vv++;
} else if (c == 'o') {
oo++;
}
map[i][j] = c;
}
}
for (int i = 0; i < rr; i++) {
for (int j = 0; j < cc; j++) {
if (map[i][j] == 'v') {
if (visited[i][j]) continue;
bfs(i, j);
}
}
}
System.out.println(oo+" "+vv);
}
static void bfs(int y, int x) {
Queue<Pair> q = new LinkedList<>();
q.add(new Pair(y, x));
int o = 0, v = 0;
while (!q.isEmpty()) {
Pair p = q.poll();
int r = p.r;
int c = p.c;
for (int i = 0; i < 4; i++) {
int nr = r + dr[i];
int nc = c + dc[i];
if (OOB(nr, nc)) continue;
if (visited[nr][nc]) continue;
if (map[nr][nc] == '#') continue;
if (map[nr][nc] == 'o') {
o++;
} else if (map[nr][nc] == 'v') {
v++;
}
visited[nr][nc] = true;
q.add(new Pair(nr, nc));
}
}
if (o > v) {
vv = vv - v;
} else {
oo = oo - o;
}
}
static boolean OOB(int r, int c) {
return r < 0 || c < 0 || r >= rr || c >= cc;
}
}