알고리즘 풀이/백준

★ [백준][Java] 2447번 별 찍기 - 10 (분할정복, 재귀) #2

배게 2021. 10. 2. 22:28
728x90

#2

자잘한 실수(무시해도됨)

private static char[][] res (star 결과 저장하는 char배열)을 선언을 안해줌

 

star메소드에서 Line:16 N==3으로 할지 N==1로 해야할지 헷갈림

잘 맞춰주면됨

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
 
public class Main {
    
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 
    private static char[][] res;
    
    private static void star(int x, int y, int N) {
        if(N==3) {
            drawStar(x, y);
            return ;
        }
        
        N/=3;
        for(int i=0; i<3; i++) {
            for(int j=0; j<3; j++) {
                if!(i==1 && j==1) ) {
                    star(x+i*N, y+j*N, N);
                }
                else
                    fillBlank(x+i*N, y+j*N, N);
            }
        }
        
    }
    
    private static void drawStar(int x, int y) {
        for(int i=x; i<x+3; i++
            for(int j=y; j<y+3; j++) {
                if(i==x+1 && j==y+1)
                    res[i][j]=' ';
                else 
                    res[i][j]='*';
            }
                
    }
 
    private static void fillBlank(int x, int y, int N) {
        for(int i=x; i<x+N; i++
            for(int j=y; j<y+N; j++)
                res[i][j]=' ';
    }
 
    public static void main(String[] args) throws IOException{
        int N = stoi(br.readLine());
        res = new char[N][N];
        
        star(00, N);
        
        for (char[] arr : res) {
            for (char c : arr) {
                bw.write(c);
            }
            bw.write("\n");
        }
        
//        bw.write("");
        bw.flush();
        bw.close();
    }
    
 
    
 
 
    private static int stoi(String input) {
        return Integer.parseInt(input);
    }
}
cs

 

 

 

 

 

 

 

#1

참고 : https://st-lab.tistory.com/95

 

솔직히 좀 헷갈림..

분할정복 재귀 깨달음 얻을 때까지 다른 문제도 풀어 보고 계속 해줘야할듯

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
 
public class Main {
    
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 
    private static char[][] res;
    
    private static void star(int x, int y, int unit, boolean blank) {
        if(blank) {
            for (int i = x; i < x+unit; i++) {
                for (int j = y; j < y+unit; j++) {
                    res[i][j]=' ';
                }
            }
            return ;
        }
        
        if(unit == 1) {
            res[x][y] = (blank)? ' ':'*';
            return ;
        }
        unit /= 3;
        
        for(int i=0; i<3; i++) {
            for(int j=0; j<3; j++) {
                if(i==1 && j==1)
                    star(x+i*unit, y+j*unit, unit, true);
                else 
                    star(x+i*unit, y+j*unit, unit, false);
            }
        }
        
    }
 
    public static void main(String[] args) throws IOException{
        int N = stoi(br.readLine());
        res = new char[N][N];
        
        star(0,0,N,false);
        
        for (char[] arr : res) {
            for (char c : arr) {
                bw.write(c);
            }
            bw.write("\n");
        }
        
        bw.flush();
        bw.close();
    }
    
 
    private static int stoi(String input) {
        return Integer.parseInt(input);
    }
}
cs