https://www.acmicpc.net/problem/1074
재귀를 돌기 전 분기로 해당하는 분기를 가는 문제였다.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <limits.h>
#include <queue>
#include <set>
#include <math.h>
#include <stack>
#include <deque>
#define ll long long
using namespace std;
int cnt = 0;
void cal(int n, int startY, int startX, int r, int c) {
// 종료 조건( n== 0일 때 종료)
if (n == 0) {
return;
}
// 사분면 분기로 나눠줄까???
if (r < startY + int(pow(2, n - 1))) {
if (c < startX + int(pow(2, n - 1))) {
cal(n - 1, startY, startX, r, c);
} else {
cnt += int(pow(2, 2 * n - 2));
cal(n - 1, startY, startX + int(pow(2, n - 1)), r, c);
}
} else {
if (c < startX + int(pow(2, n - 1))) {
cnt += int(pow(2, 2 * n - 2)) * 2;
cal(n - 1, startY + int(pow(2, n - 1)), startX, r, c);
} else {
cnt += int(pow(2, 2 * n - 2)) * 3;
cal(n - 1, startY + int(pow(2, n - 1)), startX + int(pow(2, n - 1)), r, c);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// r행(y) c열(x)
int n, r, c;
cin >> n >> r >> c;
cal(n, 0, 0, r, c);
cout << cnt;
return 0;
}
'PS > BOJ' 카테고리의 다른 글
[백준] 2206. 벽 부수고 이동하기(c++) (0) | 2025.03.11 |
---|---|
[백준] 13549. 숨바꼭질3(c++) (0) | 2025.03.11 |
[백준] 11729. 하노이 탑 이동 순서(c++) (0) | 2025.03.02 |
[백준] 1629. 곱셈(c++) (0) | 2025.03.02 |
[백준] 6593. 상범 빌딩(c++) (0) | 2025.03.02 |