Post

baekjoon 1074:Z

baekjoon 1074 Z

1074번 Z

접근

각 단계마다 절반의 height와 절반의 width를 주어진 좌표와 비교하여 상대적 위치를 파악하고
skip할 수 있는 만큼을 skip해버렸다.
그리고 이 방식을 가장 작은 2*2사각형이 나올 때 까지 했다.

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>
using namespace std;

int main(){
  int n, r, c;
  cin >> n >> r >> c;
  long long int ans = 0;
  long long int height = 1 << n;
  long long int width = 1 << n;
  while(r >=2 || c >= 2){
    height /= 2; width /= 2;
    int hCount = r / height; 
    int wCount = c / width;
    r %= height; c %= width;
    ans += hCount * height * (width << 1);
    ans += wCount * width * height;
    // cout << r << " " << c << " " << ans << " "<< height << " " << width << endl;
  }
  ans += 2*r;
  ans += c;
  cout << ans;
  
  
  return 0;
}

배운 점

This post is licensed under CC BY 4.0 by the author.