๋ถํ ์ ๋ณต(Divide and Conquer)
๋ง ๊ทธ๋๋ ๋ฌธ์ ๋ฅผ ๋ถํ ๊ณผ ์ ๋ณต์ผ๋ก ๋๋ ์ ํด๊ฒฐํ๋ ๊ฒ์ ๋งํ๋ค.
- ๋ถํ ๋จ๊ณ์์๋ ์ฃผ์ด์ง ๋ฌธ์ ๋ฅผ ์ฌ๋ฌ ๊ฐ์ ๋ถ๋ถ ๋ฌธ์ ๋ค๋ก ๋๋๋๋ฐ, ๋ฌธ์ ๊ฐ ์์์ง์๋ก ํ๊ธฐ ์ฌ์์ง๊ธฐ ๋๋ฌธ์ด๋ค.
- ๋์ฒด๋ก ๋ถํ ์ ๋ณต์ ์ฌ๊ท ํธ์ถ์ ์ฌ์ฉํ๋ค.
๋ฌธ์
https://www.acmicpc.net/problem/1992
#include <iostream>
#include <cmath>
using namespace std;
int board[64][64];
void f(int n, int x, int y)
{
bool color = board[y][x];
bool diff = false;
for (int i = y; i < n + y; ++i) {
for (int j = x; j < n + x; ++j)
if (color != board[i][j]) {
diff = true;
break;
}
if (diff)
break;
}
if (!diff) {
cout << color;
return;
}
cout << "(";
f(n / 2, x, y);
f(n / 2, x + n / 2, y);
f(n / 2, x, y + n / 2);
f(n / 2, x + n / 2, y + n / 2);
cout << ")";
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
scanf("%1d", &board[i][j]);
f(n, 0, 0);
}
https://www.acmicpc.net/problem/1780
#include <iostream>
using namespace std;
int board[2190][2190];
int ans[3];
void f(int n, int x, int y)
{
if (n == 1) {
ans[board[y][x] + 1]++;
return;
}
int tmp = board[y][x];
bool diff = false;
for (int i = y; i < n + y; ++i) {
for (int j = x; j < n + x; ++j)
if (tmp != board[i][j]) {
diff = true;
break;
}
if (diff)
break;
}
if (!diff) {
ans[tmp + 1]++;
return;
}
n /= 3;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
f(n, x + n * j, y + n * i);
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
scanf("%d", &board[i][j]);
f(n, 0, 0);
cout << ans[0] << "\n" << ans[1] << "\n" << ans[2];
}