PS

백준 1074 Z

남마허 2021. 4. 2. 04:31

www.acmicpc.net/problem/1074

 

 

 

<접근방법>

문제를 작은 문제로 쪼갤 수 있음을 알 수 있다.

 

 

 


 

 

 

<코드>

#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;

int n;
int r, c;
int res;

void find(int len, int sy, int sx, int startNum) {
	if (sy == r && sx == c) {
		res = startNum;
		return;
	}

	int half = len / 2;
	int y = sy + half;
	int x = sx + half;

	if (r < y && c < x) {
		find(half, sy, sx, startNum);
	}
	else if (r < y && c >= x) {
		find(half, sy, x, startNum + half*half);
	}
	else if (r >= y && c < x) {
		find(half, y, sx, startNum + half * half * 2);
	}
	else {
		find(half, y, x, startNum + half * half * 3);
	}
}

int main() {
	cin >> n >> r >> c;

	find(pow(2, n), 0, 0, 0);

	cout << res << '\n';
}

 

이분의 코드가 재밌다.

jaimemin.tistory.com/718

반응형