한수는 크기가 2N × 2N인 2차원 배열을 Z모양으로 탐색하려고 한다. 예를 들어, 2×2배열을 왼쪽 위칸, 오른쪽 위칸, 왼쪽 아래칸, 오른쪽 아래칸 순서대로 방문하면 Z모양이다.
N > 1인 경우, 배열을 크기가 2N-1 × 2N-1로 4등분 한 후에 재귀적으로 순서대로 방문한다.
다음 예는 22 × 22 크기의 배열을 방문한 순서이다.
N이 주어졌을 때, r행 c열을 몇 번째로 방문하는지 출력하는 프로그램을 작성하시오.
다음은 N=3일 때의 예이다.
첫째 줄에 정수 N, r, c가 주어진다.
r행 c열을 몇 번째로 방문했는지 출력한다.
n, r, c = map(int, input().split())
arr = [[0]*(2**n) for _ in range(2**n)]
ans = 0
def dac(x, y, n):
global cnt
if n == 1:
for dx, dy in [(0,0), [0,1], [1,0], [1,1]]:
cnt += 1
nx, ny = x + dx, y + dy
arr[nx][ny] = cnt
else:
n -= 1
dac(x, y, n)
dac(x, y+(2**n), n)
dac(x+(2 ** n), y, n)
dac(x+(2 ** n), y+(2 ** n), n)
cnt = -1
dac(0, 0, n)
print(arr[r][c])
n, r, c = map(int, input().split())
ans = 0
while n != 0:
n -= 1
check = 2**n
if r < check and c < check:
ans += 0
elif r < check and c >= check:
ans += check * check
c -= check
elif r >=check and c < check:
ans += 2 * check * check
r -= check
else:
ans += 3 * check * check
r -= check
c -= check
print(ans)