[백준] 1992 쿼드 트리 (실버 1)

AI·2025년 9월 10일
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    static char[][] arr; // 영상
    static StringBuilder sb = new StringBuilder();
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());
        arr = new char[n][n];
        for(int i=0;i<n;i++){
            String line = br.readLine().trim();
            for(int j=0;j<n;j++){
                arr[i][j] = line.charAt(j);
            }
        }

        QuadTree(0,0,n);
        System.out.println(sb.toString());
    }

    // 쪼개서 전부 0 혹은 1 인지 => 아니면 쪼개기
    static void QuadTree(int x, int y, int size){
        if(compare(x,y,size)){
            sb.append(arr[x][y]);
            return;
        }

        int half = size/2;
        sb.append("(");
        QuadTree(x,y,half);
        QuadTree(x,y+half,half);
        QuadTree(x+half,y,half);
        QuadTree(x+half,y+half,half);
        sb.append(")");
    }

    static boolean compare(int x, int y, int size){
        char start = arr[x][y];

        for(int i=x;i<x+size;i++){
            for(int j=y;j<y+size;j++){
                if(start != arr[i][j]){
                    return false;
                }
            }
        }
        return true;
    }
}

==

public class Main {
    static int N;
    static char[][] map;
    static StringBuilder sb = new StringBuilder();
    
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
//      map = new char[N][N]; // 공간 낭비 발생 <= toCharArray()
        map = new char[N][];
        
        for (int i = 0; i < N; i++) { // 행
            map[i] = br.readLine().toCharArray();
        }
        divide(0, 0, N);
        System.out.println(sb);
    }
    static boolean check(int y, int x, int n) {
        char ch = map[y][x]; // 검사 시작 위치의 문자
        
        for (int i = y; i < y + n; i++) {
            for (int j = x; j < x + n; j++) {
                if( ch != map[i][j] ) return false; // 즉시 return false
            }
        }
        
        return true;
    }
    
    static void divide(int y, int x, int n) {
    
        if( check(y, x, n) ) { // 모두가 같은 문자이면
            
            sb.append(map[y][x]); // 시작 문자 1개 추가
            
        }else { // 모두가 같은 문자는 아니다.
            sb.append("(");
            
            // 4 영역으로 나누어서 동일한 작업을 수행
            int half = n / 2;
            
            divide(y, x, half); // 왼쪽 위
            divide(y, x + half, half); // 오른쪽 위
            divide(y + half, x, half); // 왼쪽 아래
            divide(y + half, x + half, half); // 오른쪽 아래
            
            sb.append(")");
        }
    
    }
}

top-down

public class Main {
    static int N;
    static char[][] map;
    
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
//      map = new char[N][N]; // 공간 낭비 발생 <= toCharArray()
        map = new char[N][];
        
        for (int i = 0; i < N; i++) { // 행
            map[i] = br.readLine().toCharArray();
        }
        System.out.println(divide(0, 0, N));
    }
    static String divide(int y, int x, int n) {
        // 길이가 1인 n, 가장 바닥, 1문자
        if( n == 1 ) return String.valueOf(map[y][x]);
        
        // 아직 길이가 1이 아닌 n
        // 4 영역으로 나누어서 동일한 작업을 수행
        int half = n / 2;
        
        String ret1 = divide(y, x, half); // 왼쪽 위
        String ret2 = divide(y, x + half, half); // 오른쪽 위
        String ret3 = divide(y + half, x, half); // 왼쪽 아래
        String ret4 = divide(y + half, x + half, half); // 오른쪽 아래     
        
        // 하위 4영역이 모두 같은 문자
        // 한 문자로 모두가 같을 때 한 문자로 리턴 ( ret1.length() == 1 이 필요 )
        if( ret1.length() == 1 && ret1.equals(ret2) && ret1.equals(ret3) && ret1.equals(ret4) ) return ret1;
        else {
            StringBuilder sb= new StringBuilder();
            sb.append("(").append(ret1).append(ret2).append(ret3).append(ret4).append(")");
            return sb.toString();
        }
    }
}

0개의 댓글