-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
82 lines (70 loc) · 2.26 KB
/
Main.java
File metadata and controls
82 lines (70 loc) · 2.26 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.concurrent.DelayQueue;
public class Main {
static int count=0;
static int [][] vistied ;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
ArrayList<Integer>[] MAP = new ArrayList[n];
vistied = new int[n][n];
for (int i = 0; i < n; i++) {
MAP[i] = new ArrayList<>();
}
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
String input = st.nextToken();
for (int j = 0; j < input.length(); j++) {
MAP[i].add(Integer.parseInt(String.valueOf(input.charAt(j))));
}
}
int MapCnt=0;
Deque<Integer> answer= new LinkedList<>();
for(int i=0;i<n;i++){
for(int j=0; j<n;j++){
if(MAP[i].get(j)==1){
if (vistied[i][j]==0){
vistied[i][j]=1;
count= 1;
Find(MAP,i,j,n);
MapCnt+=1;
answer.offer(count);
}
}
}
}
int [] result = new int [MapCnt];
System.out.println(MapCnt);
int k=0;
while (!(answer.isEmpty())){
result[k]= answer.pollFirst();
k++;
}
Arrays.sort(result);
for (int re:
result) {
System.out.println(re);
}
}
static void Find(ArrayList<Integer>[] MAP, int i, int j, int n ) {
int [] dc={0,0,-1,1};
int [] dr={1,-1,0,0};
for(int k=0;k<4;k++){
int nc=i+dc[k];
int nr=j+dr[k];
if((0<=nc&&nc<n)&&(0<=nr&&nr<n)){
if (MAP[nc].get(nr)==1){
if(vistied[nc][nr]==0){
vistied[nc][nr]=1;
count+=1;
Find(MAP,nc,nr,n);
}
}
}
}
}
}