There is a square grid of size $n \times n$. Some cells are colored in black, all others are colored in white. In one operation you can select some rectangle and color all its cells in white. It costs $\max(h, w)$ to color a rectangle of size $h \times w$. You are to make all cells white for minimum total cost.
Input
The first line contains a single integer $n$ ($1 \leq n \leq 50$) — the size of the square grid.
Each of the next $n$ lines contains a string of length $n$, consisting of characters ‘.’ and ‘#’. The $j$-th character of the $i$-th line is ‘#’ if the cell with coordinates $(i, j)$ is black, otherwise it is white.
Output
Print a single integer — the minimum total cost to paint all cells in white.Examplesinput
3 ### #.# ###
output
3
input
3 ... ... ...
output
0
input
4 #... .... .... #...
output
2
input
5 #...# .#.#. ..... .#... #....
output
5
Note
The examples and some of optimal solutions are shown on the pictures below.
Solution:
#include <bits/stdc++.h> using namespace std; const int N = 55; int dp[N][N][N][N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<string> s(n); for (int i = 0; i < n; i++) { cin >> s[i]; } vector<vector<int>> f(n + 1, vector<int>(n + 1, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { f[i + 1][j + 1] = f[i + 1][j] + f[i][j + 1] - f[i][j] + (s[i][j] == '#'); } } auto Get = [&](int xa, int ya, int xb, int yb) { return f[xb + 1][yb + 1] - f[xa][yb + 1] - f[xb + 1][ya] + f[xa][ya]; }; for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { for (int ii = i; ii < n; ii++) { for (int jj = j; jj < n; jj++) { if (Get(i, j, ii, jj) == 0) { dp[i][j][ii][jj] = 0; continue; } int res = max(ii - i + 1, jj - j + 1); for (int k = i; k < ii; k++) { res = min(res, dp[i][j][k][jj] + dp[k + 1][j][ii][jj]); } for (int k = j; k < jj; k++) { res = min(res, dp[i][j][ii][k] + dp[i][k + 1][ii][jj]); } dp[i][j][ii][jj] = res; } } } } cout << dp[0][0][n - 1][n - 1] << '\n'; return 0; }