문제 : https://www.acmicpc.net/problem/1074
[ 알고리즘풀이 ]
재귀함수를 이용해 영역을 4등분으로 나눠가면서, 탐색할 영역 시작 좌표, 영역 길이, 시작 인덱스를 넘겨주면 된다.
#include<iostream>
#include<cmath>
using namespace std;
int N, r, c;
void visit(int x, int y, int length, int i) {
if (length == 1) {
if (x == r && y == c)
cout << i;
return;
}
int j = length / 2;
visit(x, y, j, i);
visit(x, y + j, j, i + j * j);
visit(x + j, y, j, i + 2 * j * j);
visit(x + j, y + j, j, i + 3 * j * j);
return;
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> N >> r >> c;
int L = int(pow(2, N));
visit(0, 0, L, 0);
}
'Problem Solving > BOJ' 카테고리의 다른 글
[BOJ] 15683 : 감시 - travelbeeee (0) | 2020.02.20 |
---|---|
[BOJ] 1074 : Z (코드개선) - travelbeeee (0) | 2020.02.19 |
[BOJ] 1124 : 언더프라임 - travelbeeee (0) | 2020.02.19 |
[BOJ] 14891 : 톱니바퀴 - travelbeeee (0) | 2020.02.19 |
[BOJ] 1500 :최대 곱 -travelbeeee (0) | 2020.02.19 |