문제
N×N크기의 행렬로 표현되는 종이가 있다. 종이의 각 칸에는 -1, 0, 1의 세 값 중 하나가 저장되어 있다. 우리는 이 행렬을 적절한 크기로 자르려고 하는데, 이 때 다음의 규칙에 따라 자르려고 한다.
- 만약 종이가 모두 같은 수로 되어 있다면 이 종이를 그대로 사용한다.
- (1)이 아닌 경우에는 종이를 같은 크기의 9개의 종이로 자르고, 각각의 잘린 종이에 대해서 (1)의 과정을 반복한다.
이와 같이 종이를 잘랐을 때, -1로만 채워진 종이의 개수, 0으로만 채워진 종이의 개수, 1로만 채워진 종이의 개수를 구해내는 프로그램을 작성하시오.
입력
첫째 줄에 N(1≤N≤3^7, N은 3^k 꼴)이 주어진다. 다음 N개의 줄에는 N개의 정수로 행렬이 주어진다.
출력
첫째 줄에 -1로만 채워진 종이의 개수를, 둘째 줄에 0으로만 채워진 종이의 개수를, 셋째 줄에 1로만 채워진 종이의 개수를 출력한다.
예제 입력 1 복사
9
0 0 0 1 1 1 -1 -1 -1
0 0 0 1 1 1 -1 -1 -1
0 0 0 1 1 1 -1 -1 -1
1 1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0
0 1 -1 0 1 -1 0 1 -1
0 -1 1 0 1 -1 0 1 -1
0 1 -1 1 0 -1 0 1 -1
예제 출력 1 복사
10
12
11
출처: <https://www.acmicpc.net/problem/1780>
/*
Problem : 종이의 개수
Writer : JaeIn Mun
Final revision : 2018.05.05
Reference :
https://www.acmicpc.net/problem/11728
http://jaeonysos.tistory.com
*/
#include <stdio.h>
#include <stdlib.h>
typedef enum {false,true} bool;
int a[3000][3000];
int cnt[3];
bool same(int x, int y, int n) {
for (int i = x; i<x + n; i++) {
for (int j = y; j<y + n; j++) {
if (a[x][y] != a[i][j]) {
return false;
}
}
}
return true;
}
void solve(int x, int y, int n) {
if (same(x, y, n)) {
cnt[a[x][y] + 1] += 1;
return;
}
int m = n / 3;
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++) {
solve(x + i * m, y + j * m, m);
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i<n; i++) {
for (int j = 0; j<n; j++) {
scanf("%d", &a[i][j]);
}
}
solve(0, 0, n);
for (int i = 0; i<3; i++) {
printf("%d\n", cnt[i]);
}
return 0;
}
*분할 정복
'Computer Science > Algorithm' 카테고리의 다른 글
[Algorithm/C] BOJ.1654 랜선 자르기 (0) | 2018.05.14 |
---|---|
[Algorithm/C] BOJ.11729 하노이 탑 이동 순서 (0) | 2018.05.09 |
[Algorithm/C] codeground 정수 정렬하기(10000) (0) | 2018.04.29 |
[Algorithm/C] BOJ.2875 대회or인턴 (0) | 2018.04.28 |
[Algorithm/C] BOJ.11399 ATM (0) | 2018.04.27 |