
문제 링크 : https://www.acmicpc.net/problem/2630
2630번: 색종이 만들기
첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다.
www.acmicpc.net
문제
색종이 수를 세는 문제. 자세한 내용은 위의 링크를 참고.
접근방식
쿼드 트리 문제와 똑같은 문제다. base case에서 1인 경우 파란색 색종이 변수의 수를 1 증가시키고,
0인 경우에는 하얀색 색종이 변수의 수를 1증가 시키면 된다.
단, 입력에 공백이 있어서 char이 아닌 int형 배열을 이용했다.
코드
#include<cstdio>
int paper[128][128];
int numOfBlue = 0;
int numOfWhite = 0;
void paparCutter(int y, int x, int size)
{
int flag = 0;
int head = paper[y][x];
for (int dy = 0; dy < size; dy++) {
for (int dx = 0; dx < size; dx++) {
if (paper[y + dy][x + dx] != head) {
flag = 1;
break;
}
}
if (flag == 1) break;
}
if (flag != 1) {
if (head == 1) numOfBlue += 1;
else numOfWhite += 1;
return;
}
else {
int half = size / 2;
paparCutter(y, x, half);
paparCutter(y, x + half, half);
paparCutter(y + half, x, half);
paparCutter(y + half, x + half, half);
return;
}
}
int main()
{
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++) {
for (int j = 0; j < t; j++) {
scanf("%d", &paper[i][j]);
}
}
paparCutter(0, 0, t);
printf("%d\n%d\n", numOfWhite, numOfBlue);
return 0;
}

다른 문제의 코드 : https://github.com/DaeeYong/Algorithm-Solution-
DaeeYong/Algorithm-Solution-
Solution for Algorithm Problem. Contribute to DaeeYong/Algorithm-Solution- development by creating an account on GitHub.
github.com
'알고리즘 > 백준 문제풀이' 카테고리의 다른 글
[C/C++]백준 2752번 - 세수정렬 (0) | 2021.10.18 |
---|---|
[C/C++]백준10808번 - 알파벳 개수 (0) | 2021.10.18 |
[C/C++]백준 1992번 - 쿼드트리 (0) | 2021.09.22 |
[C/C++]백준 2503번 - 숫자야구 (0) | 2021.09.01 |
[C/C++]백준 2579번 - 계단 오르기 (0) | 2021.08.09 |

문제 링크 : https://www.acmicpc.net/problem/2630
2630번: 색종이 만들기
첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다.
www.acmicpc.net
문제
색종이 수를 세는 문제. 자세한 내용은 위의 링크를 참고.
접근방식
쿼드 트리 문제와 똑같은 문제다. base case에서 1인 경우 파란색 색종이 변수의 수를 1 증가시키고,
0인 경우에는 하얀색 색종이 변수의 수를 1증가 시키면 된다.
단, 입력에 공백이 있어서 char이 아닌 int형 배열을 이용했다.
코드
#include<cstdio>
int paper[128][128];
int numOfBlue = 0;
int numOfWhite = 0;
void paparCutter(int y, int x, int size)
{
int flag = 0;
int head = paper[y][x];
for (int dy = 0; dy < size; dy++) {
for (int dx = 0; dx < size; dx++) {
if (paper[y + dy][x + dx] != head) {
flag = 1;
break;
}
}
if (flag == 1) break;
}
if (flag != 1) {
if (head == 1) numOfBlue += 1;
else numOfWhite += 1;
return;
}
else {
int half = size / 2;
paparCutter(y, x, half);
paparCutter(y, x + half, half);
paparCutter(y + half, x, half);
paparCutter(y + half, x + half, half);
return;
}
}
int main()
{
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++) {
for (int j = 0; j < t; j++) {
scanf("%d", &paper[i][j]);
}
}
paparCutter(0, 0, t);
printf("%d\n%d\n", numOfWhite, numOfBlue);
return 0;
}

다른 문제의 코드 : https://github.com/DaeeYong/Algorithm-Solution-
DaeeYong/Algorithm-Solution-
Solution for Algorithm Problem. Contribute to DaeeYong/Algorithm-Solution- development by creating an account on GitHub.
github.com
'알고리즘 > 백준 문제풀이' 카테고리의 다른 글
[C/C++]백준 2752번 - 세수정렬 (0) | 2021.10.18 |
---|---|
[C/C++]백준10808번 - 알파벳 개수 (0) | 2021.10.18 |
[C/C++]백준 1992번 - 쿼드트리 (0) | 2021.09.22 |
[C/C++]백준 2503번 - 숫자야구 (0) | 2021.09.01 |
[C/C++]백준 2579번 - 계단 오르기 (0) | 2021.08.09 |