1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <iostream> using namespace std; int ans = 100; int map[10][10]; int type[5] = {5, 5, 5, 5, 5 }; void Update(int x, int y, int size, int status) { for (int i = x; i < x + size; ++i) { for (int j = y; j < y + size; ++j) { map[i][j] = status; } } } bool Check(int x, int y, int size) { if (x + size > 10 || y + size > 10) return false; for (int i = x; i < x + size; ++i) { for (int j = y; j < y + size; ++j) { if (map[i][j] == 0) return false; } } return true; } void DFS(int n, int cnt) { if (n == 100) { if (ans > cnt) ans = cnt; return; } int x = n / 10; int y = n % 10; if (ans <= cnt) return;
if (map[x][y] == 1) { for (int i = 5; i > 0; --i) { if (type[i-1] > 0) { if (Check(x, y, i)) { type[i - 1]--; Update(x, y, i, 0); DFS(n+1, cnt + 1); Update(x, y, i, 1); type[i - 1]++; } } } } else DFS(n + 1, cnt); } int main() { for (int i = 0; i < 10; ++i) { for (int j = 0; j < 10; ++j) { cin >> map[i][j]; } } DFS(0, 0); if (ans == 100) ans = -1; cout << ans << "\n"; return 0; }
|