백준 Silver1 #1074 Z

김리나·2023년 3월 25일
0

알고리즘

목록 보기
6/8

문제

입력

첫째 줄에 정수 N, r, c가 주어진다.

출력

r행 c열을 몇 번째로 방문했는지 출력한다.

제한

  • 1 ≤ N ≤ 15
  • 0 ≤ r, c < 2N

예시


풀이

#include <stdio.h>
#include <math.h>

int Z(int N, int r, int c) { //N=2
	int length = (int)pow(2, N); //4
	int next = length / 2; //2

	if (N == 0) return 0;

	if (r < next && c < next) { // 0번
		return (int)pow(next, 2) * 0 + Z(N - 1, r, c);
	}
	else if (r < next && c >= next) { //1번
		return (int)pow(next, 2) * 1 + Z(N - 1, r, c - next);
	}
	else if (r >= next && c < next) { //2번
		return (int)pow(next, 2) * 2 + Z(N - 1, r - next, c);
	}
	else if (r >= next && c >= next) { //3번
		return (int)pow(next, 2) * 3 + Z(N - 1, r - next, c - next);
	}

}

int main() {
	int N, r, c;
	scanf_s("%d %d %d", &N, &r, &c);
	printf("%d", Z(N, r, c));
	return 0;
}

0개의 댓글