1+ #include < iostream>
2+ #include < vector>
3+ using namespace std ;
4+
5+ const int N = 10 ;
6+ const int INF = 987654321 ;
7+
8+ int board[N][N];
9+ vector<int > stock (6 , 5 );
10+ int bestAns = INF ;
11+
12+ bool canPlace (int y, int x, int k) {
13+ if (y + k > N || x + k > N)
14+ return false ;
15+ for (int i = 0 ; i < k; ++i)
16+ for (int j = 0 ; j < k; ++j)
17+ if (board[y + i][x + j] == 0 )
18+ return false ;
19+ return true ;
20+ }
21+
22+ void fillSquare (int y, int x, int k, int val) {
23+ for (int i = 0 ; i < k; ++i)
24+ for (int j = 0 ; j < k; ++j)
25+ board[y + i][x + j] = val;
26+ }
27+
28+ bool findNextOne (int &y, int &x) {
29+ for (int i = 0 ; i < N; ++i)
30+ for (int j = 0 ; j < N; ++j)
31+ if (board[i][j] == 1 ) {
32+ y = i;
33+ x = j;
34+ return true ;
35+ }
36+ return false ;
37+ }
38+
39+ void dfs (int used) {
40+ if (used >= bestAns)
41+ return ;
42+
43+ int y, x;
44+ if (!findNextOne (y, x)) {
45+ bestAns = min (bestAns, used);
46+ return ;
47+ }
48+
49+ for (int k = 5 ; k >= 1 ; --k) {
50+ if (stock[k] == 0 )
51+ continue ;
52+ if (!canPlace (y, x, k))
53+ continue ;
54+ fillSquare (y, x, k, 0 );
55+ stock[k]--;
56+ dfs (used + 1 );
57+ stock[k]++;
58+ fillSquare (y, x, k, 1 );
59+ }
60+ }
61+
62+ int main () {
63+ ios::sync_with_stdio (false );
64+ cin.tie (nullptr );
65+
66+ for (int i = 0 ; i < N; ++i)
67+ for (int j = 0 ; j < N; ++j)
68+ cin >> board[i][j];
69+
70+ dfs (0 );
71+
72+ cout << (bestAns == INF ? -1 : bestAns) << ' \n ' ;
73+ return 0 ;
74+ }
0 commit comments