https://www.acmicpc.net/problem/1074
분할과 정복, 재귀
#include <iostream>
#include <math.h>
using namespace std;
int N = 0, r = 0, c = 0;
int result = 0;
//x,y : 칸 시작 좌표
//n : 칸 크기
void divide(int x, int y, int n)
{
// n=1일 때
if (x == r && y == c)
{
cout << result;
exit(0);
}
//그 칸 안에 있는지 확인
if (x <= r && r < x + n && y <= c && c < y + n)
{
//칸 안에 있으면 쪼개기
divide(x, y, n / 2);
divide(x, y + n / 2, n / 2);
divide(x + n / 2, y, n / 2);
divide(x + n / 2, y + n / 2, n / 2);
}
else
{
//칸 안에 없으면 칸 내 블록개수 만큼 더함
result += n * n;
}
}
int main()
{
cin >> N >> r >> c;
divide(0, 0, pow(2, N));
}
r
, c
좌표는 그대로 두되x
,y
)를 변의 크기(n
)만큼 계속 더해서 업데이트!