문제설명
2N*2N의 2차원 배열을 z모양으로 탐색할 때 특정위치의 원소가 몇번째로 탐색되는지 구하는 문제입니다.
작동 순서
1. 배열의 크기와 몇번째로 탐색되는지 찾을 특정위치를 입력받습니다.
2. 맵 전체로 탐색을 시작합니다.
3. 맵을 4등분했을 때 찾는 행이 중간 아래의 행을 찾을 경우 위쪽 두 부분은 검색을 수행하지않고 두 부분의 원소의 개수를 count에 더해줍니다.
4. 맵을 4등분 했을 때 찾는 열이 중간 이후의 열을 찾을 경우 앞쪽 부분은 검색을 수행하지 않고 그 부분의 원소의 개수를 count에 더해줍니다.
5. 범위를 계속해서 줄여나가면서 한칸씩 나뉘어 졌을 경우 그 부분을 탐색하고 count에 +1을 해줍니다.
6. 범위를 탐색하다가 원하는 위치를 찾았으면 그 위치를 탐색하고 몇번째로 탐색했는지를 출력합니다.
소스코드
import sys
def search(rowStart, rowEnd, colStart, colEnd, size):
global count
if size == 1:
count += 1
if rowStart == r and colStart == c:
print(count, end="")
else:
if r > (rowStart+size//2)-1:
count += ((size//2)**2)*2
if c > (colStart+size//2)-1:
count += (size//2)**2
search((rowEnd-size//2)+1, rowEnd, (colEnd-size//2)+1, colEnd, size//2)
else:
search((rowEnd-size//2)+1, rowEnd, colStart, (colStart+size//2)-1, size//2)
else:
if c > (colStart+size//2)-1:
count += (size//2)**2
search(rowStart, (rowStart+size//2)-1, (colEnd-size//2)+1, colEnd, size//2)
else:
search(rowStart, (rowStart+size//2)-1, colStart, (colStart+size//2)-1, size//2)
N, r, c = map(int, sys.stdin.readline().split())
count = -1
search(0, 2**N-1, 0, 2**N-1, 2**N)