-
Notifications
You must be signed in to change notification settings - Fork 0
/
prob2583.java
107 lines (97 loc) · 2.54 KB
/
prob2583.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
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
package Algorithm_0812;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;
//백준 2583 영역 구하기
public class prob2583 {
private static int N, M, K, tmp;
private static int[][] map;
private static boolean[][] visited;
private static void print() {
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(map[i][j]+" ");
}
System.out.println();
}
}
private static class point {
int x; int y;
point(int x, int y){
this.x = x;
this.y = y;
}
}
private static boolean isInBound(int x,int y,int M,int N) {
return x>=0 && y>=0 && x<M && y<N;
}
private static void BFS(int x, int y) {
tmp = 1;
Queue<point> queue = new LinkedList<>();
queue.offer(new point(x,y));
visited[x][y] = true;
int[] dx = {-1,0,1,0};
int[] dy = {0,1,0,-1};
while(!queue.isEmpty()) {
point current = queue.poll();
for (int i = 0; i < 4; i++) {
int a = current.x + dx[i];
int b = current.y + dy[i];
if(isInBound(a,b,M,N) && !visited[a][b] && map[a][b]==0) {
queue.offer(new point(a,b));
visited[a][b] = true;
tmp++;
}
}
}
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine()," ");
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
map = new int[M][N];
visited = new boolean[M][N];
for (int i = 0; i < K; i++) {
st = new StringTokenizer(bf.readLine()," ");
int xtmp1 = Integer.parseInt(st.nextToken());
int ytmp1 = Integer.parseInt(st.nextToken());
int xtmp2 = Integer.parseInt(st.nextToken());
int ytmp2 = Integer.parseInt(st.nextToken());
int x1 = M - ytmp1 -1;
int y1 = xtmp1;
int x2 = M - ytmp2;
int y2 = xtmp2 -1;
for (int j = x2; j <= x1; j++) {
for (int j2 = y1; j2 <= y2; j2++) {
map[j][j2] = 1;
}
}
}
//print();
List<Integer> result = new ArrayList<>();
int domain = 0;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if(map[i][j]==0 && !visited[i][j]) {
BFS(i,j);
result.add(tmp);
domain++;
}
}
}
Collections.sort(result);
System.out.println(domain);
for(int x : result) {
System.out.print(x+" ");
}
}
}