문제: 1074번: Z
basic-algo-lecture/workbook/0x0B.md at master · encrypted-def/basic-algo-lecture
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip> // setprecision을 사용하기 위한 헤더
#include <climits>
#include <list>
using namespace std;
int Z(int n, int r, int c) {
if (n == 0)
return 0;
int half = pow(2, n - 1); // 현재 스텝의 n 을 2나눈 값이 half
if (r < half && c < half) return Z(n - 1, r, c);
if (r < half && c >= half) return half * half + Z(n - 1, r, c - half);
if (r >= half && c < half) return 2 * half * half + Z(n - 1, r - half, c);
return 3 * half * half + Z(n - 1, r - half, c - half);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, r, c;
cin >> n >> r >> c;
cout << Z(n, r, c);
return 0;
}
.. 결국 동영상 풀이 참고해서 풀었다.
재귀의 문제가 다 그렇지만 이 문제도 역시 이전 단계의 결과를 이용해서 현재 단계의 값을 구하는 게 핵심이다. 문제에서 4등분하라고 줬기 때문에 함수를 구현할 때 역시 4등분 해서 생각하면 해결 할 수 있는 문제였다.
문제 풀었다고 끝이 아니고 계속해서 복습해야 할 것 같다.. 재귀 너무 어렵다...
참고자료:
[바킹독의 실전 알고리즘] 0x0B강 - 재귀 - YouTube