ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [ 백준 ][ 자바 ][ 2667 ] 단지번호 붙히기
    카테고리 없음 2019. 3. 4. 22:45

    오래간만에 간단하게 술 한잔을 했다

    오늘부터 새로운 곳에 적응을 해야한다.ㅠ

    ..분위기는 나쁘지않았지만 살짝 걱정은 된다. 

    집에와서 알딸딸한 참에 제일 간단해보이는 문제를 잡고 풀었다..ㅠ

    어둑해지니 센티멘탈해지는것같다.

    이런 저런 생각에 뭔가 답답해지기도 한다.

    잘하자! 의미없는 시간은 결코 없다!

    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.Queue;
    import java.util.StringTokenizer;
    
    class dPoint{
    	int y;
    	int x;
    	dPoint(int y, int x){
    		this.y = y;
    		this.x = x;
    		
    	}
    }
    public class Main {
    	static Queue q = new LinkedList();
    	static int[] dx = {1, -1, 0, 0};
    	static int[] dy = {0 ,0 ,1, -1};
    	static int N;
    	static int houseCount;
    	static int[][] MAP = new int[26][26];
    	static ArrayList houseCountList = new ArrayList();
    	static boolean[][] visited = new boolean[26][26];
    	public static void main(String [] args) throws IOException {
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		StringTokenizer st = new StringTokenizer(br.readLine(), "");
    		N = Integer.parseInt(st.nextToken());
    		for(int i = 0; i < N; i++) {
    			String[] arr = br.readLine().split("");
    			for(int j = 0; j < arr.length; j++) {
    				MAP[i][j] = Integer.parseInt(arr[j]);
    			}
    		}
    		//printMap();
    		for(int i = 0; i < N; i++) {
    			for(int j =0; j < N; j++) {
    				if( !visited[i][j] && MAP[i][j] != 0 ) {
    					houseCountList.add(numbering(i, j));
    					houseCount++;
    				}
    			}
    		}
    		System.out.println(houseCount);
    		Collections.sort(houseCountList);
    		for(int i = 0; i < houseCountList.size(); i++) {
    			System.out.println(houseCountList.get(i));
    		}
    	
    	}
    	public static void printMap() {
    		for(int i = 0; i < N; i++) {
    			for(int j =0; j < N; j++) {
    				System.out.print(MAP[i][j] + " ");
    			}
    			System.out.println();
    		}
    	}
    	public static int numbering(int y,int x) {
    		q.add(new dPoint(y, x));
    		visited[y][x] = true;
    		int count = 1; 
    		while(!q.isEmpty()) {
    			dPoint dp = q.poll();
    			for(int i = 0 ; i < 4; i++) {
    				int xx = dp.x + dx[i];
    				int yy = dp.y + dy[i];
    				if( xx >= 0 && xx < N && yy >= 0 && yy < N) {
    					if( MAP[yy][xx] != 0 && !visited[yy][xx] ) {
    						q.add(new dPoint(yy, xx));
    						visited[yy][xx] = true;
    						count ++;
    					}		
    				}
    			}
    		}
    		return count;
    	}
    
    
    }
    
    


    댓글

Designed by Tistory.