728x90
https://www.acmicpc.net/problem/1780
분할정복으로 9등분을 해가면서 풀면 된다.
그 나눠진 부분의 전체가 같은 수라면 재귀호출을 하지 않고 나눠진 부분에 있는 수들이 모두 같지 않으면 재귀호출을 해서 또 9등분을 하는 식으로 풀었따.
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> v;
int minusOne = 0;
int zero = 0;
int one = 0;
void divide(int rowStart, int rowEnd, int colStart, int colEnd, int width) {
int first = v[rowStart][colStart];
bool flag = true;
if (width != 1) {
for (int i = rowStart; i <= rowEnd; i++) {
for (int j = colStart; j <= colEnd; j++) {
if (first != v[i][j]) {
flag = false;
break;
}
}
if (!flag)
break;
}
}
if (flag) {
if (first == -1) {
minusOne++;
}
else if (first == 0) {
zero++;
}
else if (first == 1) {
one++;
}
}
else {
divide(rowStart, rowStart + width / 3 - 1, colStart, colStart + width / 3 - 1, width / 3);
divide(rowStart + width / 3, rowStart + width / 3 + width / 3 - 1, colStart, colStart + width / 3 - 1, width / 3);
divide(rowStart + width / 3 + width / 3, rowEnd, colStart, colStart+ width / 3 - 1, width / 3);
divide(rowStart, rowStart + width / 3 - 1, colStart + width / 3, colStart + width / 3 + width / 3 - 1, width / 3);
divide(rowStart + width / 3, rowStart + width / 3 + width / 3 - 1, colStart + width / 3, colStart + width / 3 + width / 3 - 1, width / 3);
divide(rowStart + width / 3 + width / 3, rowEnd, colStart + width / 3, colStart + width / 3 + width / 3 - 1, width / 3);
divide(rowStart, rowStart + width / 3 - 1, colStart + width / 3 + width / 3, colEnd, width / 3);
divide(rowStart + width / 3, rowStart + width / 3 + width / 3 - 1, colStart + width / 3 + width / 3, colEnd, width / 3);
divide(rowStart + width / 3 + width / 3, rowEnd, colStart + width / 3 + width / 3, colEnd, width / 3);
}
}
int main() {
int n;
cin >> n;
v = vector<vector<int>>(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> v[i][j];
}
}
divide(0, n - 1, 0, n - 1, n);
cout << minusOne << endl;
cout << zero << endl;
cout << one << endl;
return 0;
}
728x90
'알고리즘 문제' 카테고리의 다른 글
[프로그래머스] 땅따먹기 (0) | 2020.07.30 |
---|---|
[프로그래머스] 폰켓몬 (0) | 2020.07.28 |
[백준] N M 찍기 (0) | 2020.04.11 |
[백준] 10815번 숫자 카드 (0) | 2020.04.11 |
[백준] 1158번 요세푸스 문제 (0) | 2020.04.11 |