[백준] 1074 Z

0

백준

목록 보기
9/271
post-thumbnail
post-custom-banner

백준 1074 Z

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

int getResult(int N, int r, int c) {
	//base case
	if (N == 1) {
		if (r == 0 && c == 0) return 0;
		if (r == 0 && c == 1) return 1;
		if (r == 1 && c == 0) return 2;
		if (r == 1 && c == 1) return 3;
	}

	int half = pow(2, N - 1);
	int full = pow(2, N);

	if (r < half){
		//upperLeft
		if (c < half) 
			return getResult(N - 1, r, c) + half * half * 0;
		//upperRight
		else if (c < full) 
			return getResult(N - 1, r, c - half) + half * half * 1;	
	}
	else if (r < full) {
		//lowerLeft
		if (c < half) 
			return getResult(N - 1, r - half, c) + half * half * 2;
		//lowerRight
		else if (c < full) 
			return getResult(N - 1, r - half, c - half) + half * half * 3;
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int N, r, c;
	cin >> N >> r >> c;

	cout << getResult(N, r, c);

	return 0;
}
profile
Be able to be vulnerable, in search of truth
post-custom-banner

0개의 댓글