알고리즘 풀이/백준

★ [백준][Java] 1074번 Z (분할정복, 재귀)

배게 2021. 10. 2. 21:25
728x90

스스로 못 풀었음

분할정복이란 것을 좀 더 연구해봐야겠음 

 

참고 : https://bcp0109.tistory.com/47

 

 

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
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));
 
    public static void main(String[] args) throws IOException{
        String[] str = br.readLine().split(" ");
        int N = 1;
        for(int i=0; i<stoi(str[0]); i++)
            N *= 2;
        
        int r = stoi(str[1]);
        int c = stoi(str[2]);
        int x=0, y=0;
        
        int res = 0;
        while(true) {
            N/=2;
            
            if(r<x+&& c<y+N) {
                res += N*N*0;
            }
            else if(r<x+N) {
                y+=N;
                res += N*N*1;
            }
            else if(c<y+N) {
                x+=N;
                res += N*N*2;
            }
            else {
                x+=N;
                y+=N;
                res += N*N*3;
            }
            
            if(N==1) {
                System.out.println(res);
                break;
            }
            
        }
    }
    
 
    private static int stoi(String input) {
        return Integer.parseInt(input);
    }
}
cs