-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboj_2583.java
More file actions
87 lines (69 loc) · 2.2 KB
/
boj_2583.java
File metadata and controls
87 lines (69 loc) · 2.2 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
package dfs_bfs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class boj_2583 {
static int M,N,K;
static int[][] arr;
static Queue<int[]> q=new LinkedList<>();
static int[] dx={-1,1,0,0};
static int[] dy={0,0,-1,1};
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
M=Integer.parseInt(st.nextToken());
N=Integer.parseInt(st.nextToken());
K=Integer.parseInt(st.nextToken());
arr=new int[N][M];
LinkedList<Integer> areas=new LinkedList<>();
for(int i=0;i<K;i++){
st=new StringTokenizer(br.readLine());
int x1=Integer.parseInt(st.nextToken());
int y1=Integer.parseInt(st.nextToken());
int x2=Integer.parseInt(st.nextToken());
int y2=Integer.parseInt(st.nextToken());
for(int x=x1;x<x2;x++){
for(int y=y1;y<y2;y++){
if(arr[x][y]!=1){
arr[x][y]=1;
}
}
}
}
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
if(arr[i][j]==0){
arr[i][j]=1;
q.add(new int[]{i,j});
areas.add(bfs());
}
}
}
Collections.sort(areas);
System.out.println(areas.size());
for(int area: areas){
System.out.print(area+" ");
}
br.close();
}
private static int bfs(){
int area=0;
while(!q.isEmpty()){
int[] cur=q.poll();
area++;
for(int i=0;i<4;i++){
int nx=cur[0]+dx[i];
int ny=cur[1]+dy[i];
if(nx>=0 && ny>=0 && nx<N && ny<M && arr[nx][ny]!=1){
q.add(new int[]{nx,ny});
arr[nx][ny]=1;
}
}
}
return area;
}
}