11import java .io .BufferedReader ;
22import java .io .IOException ;
33import java .io .InputStreamReader ;
4- import java .util .ArrayList ;
5- import java .util .List ;
6- import java .util .StringTokenizer ;
4+ import java .util .*;
5+ import java .util .concurrent .DelayQueue ;
76
87public class Main {
8+ static int count =0 ;
9+ static int [][] vistied ;
910 public static void main (String [] args ) throws IOException {
10-
1111 BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
1212 StringTokenizer st = new StringTokenizer (br .readLine ());
1313 int n = Integer .parseInt (st .nextToken ());
1414 ArrayList <Integer >[] MAP = new ArrayList [n ];
15- int [][] vistied = new int [n ][n ];
15+ vistied = new int [n ][n ];
1616 for (int i = 0 ; i < n ; i ++) {
1717 MAP [i ] = new ArrayList <>();
1818 }
@@ -27,23 +27,37 @@ public static void main(String[] args) throws IOException {
2727 }
2828 }
2929 int MapCnt =0 ;
30+ Deque <Integer > answer = new LinkedList <>();
3031 for (int i =0 ;i <n ;i ++){
3132 for (int j =0 ; j <n ;j ++){
3233 if (MAP [i ].get (j )==1 ){
3334 if (vistied [i ][j ]==0 ){
3435 vistied [i ][j ]=1 ;
35- int count =Find (MAP ,vistied ,i ,j ,n ,1 );
36+ count = 1 ;
37+ Find (MAP ,i ,j ,n );
3638 MapCnt +=1 ;
37- System . out . println (count );
39+ answer . offer (count );
3840 }
3941
4042 }
4143 }
4244 }
45+ int [] result = new int [MapCnt ];
46+ System .out .println (MapCnt );
47+ int k =0 ;
48+ while (!(answer .isEmpty ())){
49+ result [k ]= answer .pollFirst ();
50+ k ++;
51+ }
52+ Arrays .sort (result );
53+ for (int re :
54+ result ) {
55+ System .out .println (re );
56+ }
4357
4458 }
4559
46- public static int Find (ArrayList <Integer >[] MAP ,int [][] vistied , int i , int j , int n , int count ) {
60+ static void Find (ArrayList <Integer >[] MAP , int i , int j , int n ) {
4761 int [] dc ={0 ,0 ,-1 ,1 };
4862 int [] dr ={1 ,-1 ,0 ,0 };
4963 for (int k =0 ;k <4 ;k ++){
@@ -54,15 +68,15 @@ public static int Find(ArrayList<Integer>[] MAP,int[][] vistied, int i, int j, i
5468 if (vistied [nc ][nr ]==0 ){
5569 vistied [nc ][nr ]=1 ;
5670 count +=1 ;
57- Find (MAP ,vistied , nc ,nr ,n , count );
58- vistied [ nc ][ nr ]= 0 ;
71+ Find (MAP ,nc ,nr ,n );
72+
5973 }
6074
6175 }
6276 }
6377
6478 }
65- return count ;
79+
6680
6781 }
6882}
0 commit comments