본문 바로가기
Algorithm/C++

[백준 1074] Z

by imagineer_jinny 2022. 9. 7.

1074번: Z (acmicpc.net)

 

1074번: Z

한수는 크기가 2N × 2N인 2차원 배열을 Z모양으로 탐색하려고 한다. 예를 들어, 2×2배열을 왼쪽 위칸, 오른쪽 위칸, 왼쪽 아래칸, 오른쪽 아래칸 순서대로 방문하면 Z모양이다. N > 1인 경우, 배열을

www.acmicpc.net

 

배운 것

출처 하단 표시

 

정답 풀이

// http://boj.kr/fd805e1226e949f9b6b2eff59e5be642
#include <bits/stdc++.h>
using namespace std;

int func(int n, int r, int c){
  if(n == 0) return 0;
  int half = 1<<(n-1);
  if(r < half && c < half) return func(n-1, r, c);
  if(r < half && c >= half) return half*half + func(n-1, r, c-half);
  if(r >= half && c < half) return 2*half*half + func(n-1, r-half, c);
  return 3*half*half + func(n-1, r-half, c-half);
}

int main(void){
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, r, c;
  cin >> n >> r >> c;
  cout << func(n, r, c);
}

 

 

 

 

 

 

출처

BaaaaaaaarkingDog | [실전 알고리즘] 0x0B강 - 재귀 (encrypted.gg)

 

 

'Algorithm > C++' 카테고리의 다른 글

[백준 1780] 종이의 개수  (0) 2022.09.08
[백준 17478] 재귀함수가 뭔가요?  (0) 2022.09.07
[백준 1629] 곱셈  (0) 2022.09.06
[백준 6593] 상범 빌딩 (tuple 사용법)  (0) 2022.09.06
[백준 2468] 안전 영역  (0) 2022.09.06

댓글