✔ 풀이를 위한 아이디어
✔ 수정 전 코드 [시간 초과]
import sys
count = -1
def traverse(start_x, start_y, n):
global count
if n == 1:
count += 1
if start_x == c and start_y == r:
print(count)
else:
traverse(start_x, start_y, n//2)
traverse(start_x + n//2, start_y, n//2)
traverse(start_x, start_y + n//2, n//2)
traverse(start_x + n//2, start_y + n//2, n//2)
N, r, c = map(int, sys.stdin.readline().split())
traverse(0, 0, 2**N)
✔ 수정 후 코드
import sys
count = -1
def traverse(start_x, start_y, n):
global count
if n == 1:
count += 1
if start_x == c and start_y == r:
print(count)
else:
if c < start_x + n//2 and r < start_y + n//2: #1사분면
traverse(start_x, start_y, n//2)
else:
count += (n//2)**2
if c >= start_x + n//2 and r < start_y + n//2: #2사분면
traverse(start_x + n//2, start_y, n//2)
else:
count += (n//2)**2
if c < start_x + n//2 and r >= start_y + n//2: #3사분면
traverse(start_x, start_y + n//2, n//2)
else:
count += (n//2)**2
if c >= start_x + n//2 and r >= start_y + n//2: #4사분면
traverse(start_x + n//2, start_y + n//2, n//2)
else: #사실 이 else문은 지워도 무방하다.
count += (n//2)**2
N, r, c = map(int, sys.stdin.readline().split())
traverse(0, 0, 2**N)
✔ 관련 개념