[Gold V] Title: Z, Time: 44 ms, Memory: 32412 KB -BaekjoonHub

This commit is contained in:
SSUM
2025-03-24 20:49:33 +09:00
parent 672e21ed11
commit bab8f2a984
2 changed files with 69 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
# [Gold V] Z - 1074
[문제 링크](https://www.acmicpc.net/problem/1074)
### 성능 요약
메모리: 32412 KB, 시간: 44 ms
### 분류
분할 정복, 재귀
### 제출 일자
2025년 3월 24일 20:49:18
### 문제 설명
<p>한수는 크기가 2<sup>N</sup> × 2<sup>N</sup>인 2차원 배열을 Z모양으로 탐색하려고 한다. 예를 들어, 2×2배열을 왼쪽 위칸, 오른쪽 위칸, 왼쪽 아래칸, 오른쪽 아래칸 순서대로 방문하면 Z모양이다.</p>
<p style="text-align:center"><img alt="" src="https://u.acmicpc.net/21c73b56-5a91-43aa-b71f-9b74925c0adc/Screen%20Shot%202020-12-02%20at%208.09.46%20AM.png" style="width: 100px; height: 99px;"></p>
<p>N > 1인 경우, 배열을 크기가 2<sup>N-1</sup> × 2<sup>N-1</sup>로 4등분 한 후에 재귀적으로 순서대로 방문한다.</p>
<p>다음 예는 2<sup>2</sup> × 2<sup>2</sup> 크기의 배열을 방문한 순서이다.</p>
<p style="text-align:center"><img alt="" src="" style="width: 250px; height: 252px;"></p>
<p>N이 주어졌을 때, r행 c열을 몇 번째로 방문하는지 출력하는 프로그램을 작성하시오.</p>
<p>다음은 N=3일 때의 예이다.</p>
<p style="text-align:center"><img alt="" src="" style="width: 533px; height: 535px;"></p>
### 입력
<p>첫째 줄에 정수 N, r, c가 주어진다.</p>
### 출력
<p>r행 c열을 몇 번째로 방문했는지 출력한다.</p>

View File

@@ -0,0 +1,27 @@
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
N, r, c = map(int, input().split())
def squared(n, r, c, tot):
next_right, next_height = 2** n, 2** n
if(n==1 and r==0 and c==0):
return tot
elif(n==1 and r==0 and c==1):
return tot+1
elif(n==1 and r==1 and c==0):
return tot+2
elif(n==1 and r==1 and c==1):
return tot+3
if (r<next_right//2 and c <next_height//2):
return squared(n-1, r, c, tot)
elif (r<next_right//2):
return squared(n-1, r, c-(2**(n-1)), tot+2 ** (2*n-2))
elif (c<next_height//2):
return squared(n-1, r-(2**(n-1)), c, tot + 2 ** (2*n-1))
else:
return squared(n-1, r-(2**(n-1)), c-(2**(n-1)), tot + (2 ** (2*n-2) + 2 ** (2*n-1)))
print(squared(N, r, c, 0))