Point 💡
- 구하려는 좌표가 제 1사분면, 제 2사분면, 제 3사분면, 제 4사분면 어디에 위치해있는지 파악
- 각 사분면의 첫번째 수 → 2^n-1 * 2^n-1의 배수
- n=3일 때, 2사분면의 첫번째 수는 2^22^2=44=16, 3사분면의 두번째 수는 2^2 * 2^2 *2 = 4 * 4 * 2 = 32 …
- 구하려는 좌표는 포함되어있는 사분면에 따라 계속해서 4등분으로 나눴을 때 좌표값이 달라짐
- n=3일 때, 4사분면의 경우, 구하려는 좌표가 (5,7)이면, 다시 4등분으로 나눴을 때 좌표는 (5 - 2^2, 7-2^2) = (1, 3)이 됨
- n=3일 때, 2사분면의 경우, 구하려는 좌표가 (5,4)이면, 다시 4등분으로 나눴을 때 좌표는 (5 - 2^2, 4) = (1, 4)가 됨
My Solution
import sys
N, r, c = map(int, sys.stdin.readline().split())
res = 0
while N != 0:
# 4등분으로 분할
N -= 1
if r < 2**N and c < 2**N:
res += 0
elif r < 2**N and c >= 2**N:
res += (2**N) * (2**N) * 1
c -= (2**N)
elif r >= 2**N and c < 2**N:
res += (2**N) * (2**N) * 2
r -= (2**N)
else:
res += (2**N) * (2**N) * 3
r -= (2**N)
c -= (2**N)
print(res)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String [] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int res = 0;
while (N !=0) {
N -= 1;
// 제 1사분면
if (r < Math.pow(2, N) && c < Math.pow(2, N)) {
res += 0;
}
// 제 2사분면
else if (r < Math.pow(2, N) && c >= Math.pow(2, N)) {
res += (int)(Math.pow(2, N) * Math.pow(2, N));
c -= (int) Math.pow(2, N);
}
// 제 3사분면
else if (r >= Math.pow(2, N) && c < Math.pow(2, N)) {
res += (int)(Math.pow(2, N) * Math.pow(2, N) * 2);
r -= (int) Math.pow(2, N);
}
// 제 4사분면
else {
res += (int)(Math.pow(2, N) * Math.pow(2, N) * 3);
r -= (int) Math.pow(2, N);
c -= (int) Math.pow(2, N);
}
}
System.out.println(res);
}
}