import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; class Point{ int x,y; public Point(int y, int x){ this.y=y; this.x=x; } } public class Main { static int map[][]; public static void main(String[] args) throws IOException{ BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); n=Integer.parseInt(reader.readLine()); map=new int [n][n]; for(int i=0;i<n;i++){ String s=reader.readLine(); for(int j=0;j<n;j++){ map[i][j]=Integer.parseInt(s.charAt(j)+""); } } ArrayList<Integer> list=new ArrayList<Integer>(); count=0; visit=new int [n][n]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(map[i][j]==1 && visit[i][j]!=1){ //1이고, 아직방문전이면 dfs시작 dfs(new Point(i,j)); if(count==0){//1개만 섬처럼 떨어져있을때 밑에서 count++가 안되기때문에 count=1;//count=0일때 1이 되도록한다. (위에 if문에서 이미 조건 충족ok) } list.add(count); count=0; } } } System.out.println(list.size()); int arr[]=new int [list.size()]; for(int i=0;i<list.size();i++){ arr[i]=list.get(i); } Arrays.sort(arr); for(int i=0;i<arr.length;i++){ System.out.println(arr[i]); } } static int visit[][]; static int count,n; static int dx[]={0,1,0,-1}; static int dy[]={-1,0,1,0}; static void dfs(Point point){ for(int i=0;i<4;i++){ int x=point.x; int y=point.y; if(x+dx[i]<0 || x+dx[i]>n-1 || y+dy[i]<0 || y+dy[i]>n-1){ continue; } x+=dx[i]; y+=dy[i]; if(map[y][x]==1 && visit[y][x]!=1){ count++; visit[y][x]=1; dfs(new Point(y,x)); } } } }


++ 삼전 2017년 상반기 코딩 문제랑 거의 똑같..

+ Recent posts