문제 |
1074번: Z
한수는 크기가 2N × 2N인 2차원 배열을 Z모양으로 탐색하려고 한다. 예를 들어, 2×2배열을 왼쪽 위칸, 오른쪽 위칸, 왼쪽 아래칸, 오른쪽 아래칸 순서대로 방문하면 Z모양이다. N > 1인 경우, 배열을
www.acmicpc.net
코드 |
#include <iostream>
using namespace std;
int n, r, c;
void input() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> r >> c;
}
void solve(int y, int x, int size, int cnt) {
if (size == 0) {
cout << cnt;
return;
}
size--;
int d = 1 << size;
if (r >= y + d) {
y += d;
cnt += d * d * 2;
}
if (c >= x + d) {
x += d;
cnt += d * d;
}
solve(y, x, size, cnt);
}
int main() {
input();
solve(0, 0, n, 0);
return 0;
}
설명 |
주어진 배열을 4등분하여 그 4개의 위치 중 (r,c)가 속한 위치를 찾는 재귀 함수를 만든다.
'PS > BOJ' 카테고리의 다른 글
[C++] BOJ (백준) 1012 : 유기농 배추 (0) | 2023.03.16 |
---|---|
[C++] BOJ (백준) 1003 : 피보나치 함수 (0) | 2023.03.07 |
[C++] BOJ (백준) 18111 : 마인크래프트 (0) | 2023.03.06 |
[C++] BOJ (백준) 15829 : Hashing (0) | 2023.03.05 |
[C++] BOJ (백준) 11866 : 요세푸스 문제 0 (0) | 2023.03.05 |