[알고리즘/백준] #1992 쿼드트리

JudyLia·2022년 2월 16일
0

알고리즘

목록 보기
49/61
post-thumbnail

문제)

package algorithm_lab.day10.hw;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BJ_1992 {
	
	public static int N;
	public static String[][] img;
	public static StringBuilder sb = new StringBuilder();
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		N = Integer.parseInt(br.readLine());
		img=new String[N][N];
		for(int i=0;i<N;i++) {
			img[i]=br.readLine().split("");
		}
		comp(N,0,0);
		System.out.print(sb.toString());
	}
	
	public static void comp(int size, int row, int col) {
		if(check(size,row,col)) {
			sb.append(img[row][col]);
			return;
		}
		int newsize=size/2;
		sb.append("(");
		comp(newsize,row,col);
		comp(newsize,row,col+newsize);
		comp(newsize,row+newsize,col);
		comp(newsize,row+newsize,col+newsize);
		sb.append(")");
	}
	
	public static boolean check(int size, int row, int col) {
		
		String cp= img[row][col];
		
		for(int i=row;i<row+size;i++) {
			for(int j=col;j<col+size;j++) {
				if(!img[i][j].equals(cp)) return false;
			}
		}
		return true;
	}
}
profile
안녕:)

0개의 댓글

관련 채용 정보