문제 접근
예전에 비슷한 문제 푼 적 있었는데... 범위 내부 확인하고, 조건에 성립하지 않으면 재귀로 범위 나눠서 다시 확인하면 되는 간단한 문제였다.
문제 코드
#include <iostream>
using namespace std;
int plane[128][128];
int blue=0, white=0;
void count(int x, int y, int n)
{
// 해당 면이 전부 같은 색이면 true.
bool check = true;
for(int i=x; i<x+n; ++i)
for(int j=y; j<y+n; ++j)
if(plane[i][j]!=plane[x][y])
check = false;
if(check)
{
if(plane[x][y]==0)
++white;
else
++blue;
}
// 해당 면이 전부 같은 색이 아니면 4등분해서 다시 확인.
else
{
count(x,y,n/2);
count(x+n/2,y,n/2);
count(x,y+n/2,n/2);
count(x+n/2,y+n/2,n/2);
}
}
int main(void)
{
int N;
cin>>N;
for(int i=0; i<N; ++i)
for(int j=0; j<N; ++j)
cin>>plane[i][j];
count(0,0,N);
cout<<white<<'\n'<<blue<<'\n';
}
'개발 > 알고리즘' 카테고리의 다른 글
백준 1865 번 : 웜홀 (2) | 2020.09.02 |
---|---|
백준 1753번 : 최단 경로 (0) | 2020.08.29 |
백준 1167번 : 트리의 지름 (0) | 2020.08.28 |
백준 11725번 : 트리의 부모 찾기 (0) | 2020.08.28 |
백준 1967번 : 트리의 지름 (0) | 2020.08.27 |
문제 접근
예전에 비슷한 문제 푼 적 있었는데... 범위 내부 확인하고, 조건에 성립하지 않으면 재귀로 범위 나눠서 다시 확인하면 되는 간단한 문제였다.
문제 코드
#include <iostream>
using namespace std;
int plane[128][128];
int blue=0, white=0;
void count(int x, int y, int n)
{
// 해당 면이 전부 같은 색이면 true.
bool check = true;
for(int i=x; i<x+n; ++i)
for(int j=y; j<y+n; ++j)
if(plane[i][j]!=plane[x][y])
check = false;
if(check)
{
if(plane[x][y]==0)
++white;
else
++blue;
}
// 해당 면이 전부 같은 색이 아니면 4등분해서 다시 확인.
else
{
count(x,y,n/2);
count(x+n/2,y,n/2);
count(x,y+n/2,n/2);
count(x+n/2,y+n/2,n/2);
}
}
int main(void)
{
int N;
cin>>N;
for(int i=0; i<N; ++i)
for(int j=0; j<N; ++j)
cin>>plane[i][j];
count(0,0,N);
cout<<white<<'\n'<<blue<<'\n';
}
'개발 > 알고리즘' 카테고리의 다른 글
백준 1865 번 : 웜홀 (2) | 2020.09.02 |
---|---|
백준 1753번 : 최단 경로 (0) | 2020.08.29 |
백준 1167번 : 트리의 지름 (0) | 2020.08.28 |
백준 11725번 : 트리의 부모 찾기 (0) | 2020.08.28 |
백준 1967번 : 트리의 지름 (0) | 2020.08.27 |