알고리즘/BaekJoon
[c++] 2630번 색종이 만들기
__jhp_+
2022. 6. 28. 11:55
https://www.acmicpc.net/problem/2630
2630번: 색종이 만들기
첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다.
www.acmicpc.net
#include <iostream>
using namespace std;
#define MAX_SIZE 129
void CountOfPaper(int paper[][MAX_SIZE], int x, int y, int length, int* count);
bool IsSamePaper(int paper[][MAX_SIZE], int x, int y, int length);
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int paper[MAX_SIZE][MAX_SIZE] = { 0, };
int count[2] = { 0, };
int length = 0;
cin >> length;
for (int i = 1; i <= length; ++i)
{
for (int j = 1; j <= length; ++j)
{
cin >> paper[i][j];
}
}
CountOfPaper(paper, 1, 1, length, count);
for (int i = 0; i < sizeof(count) / sizeof(int); ++i)
{
cout << count[i] << '\n';
}
return 0;
}
void CountOfPaper(int paper[][MAX_SIZE], int x, int y, int length, int* count)
{
if (length == 1 || IsSamePaper(paper, x, y, length) == true)
{
int index = paper[y][x];
++count[index];
}
else
{
int half = length / 2;
CountOfPaper(paper, x, y, half, count);
CountOfPaper(paper, x, y + half, half, count);
CountOfPaper(paper, x + half, y, half, count);
CountOfPaper(paper, x + half, y + half, half, count);
}
return;
}
bool IsSamePaper(int paper[][MAX_SIZE], int x, int y, int length)
{
int value = paper[y][x];
for (int i = y; i < y + length; ++i)
{
for (int j = x; j < x + length; ++j)
{
if (paper[i][j] != value)
{
return false;
}
}
}
return true;
}