传送门
F. Rectangle Painting 1
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
There is a square grid of size n×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×w. You are to make all cells white for minimum total cost.
Input
The first line contains a single integer n (1≤n≤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.
Examples
inputCopy
3
#.#
outputCopy
3
inputCopy
3
…
…
…
outputCopy
0
inputCopy
4
#…
…
…
#…
outputCopy
2
inputCopy
5
#…#
.#.#.
…
.#…
#…
outputCopy
5
Note
The examples and some of optimal solutions are shown on the pictures below.
题意:有一个n*n大小的方格图,某些方格初始是黑色,其余为白色,要求用最小的代价把所有方格变成白色,其中代价的计算方式为染一个n * m的矩形区域的代价为max(m,n)
题解:dp[x1][y1][x2][y2]为把左下角为(x1,y1)右上角为(x2,y2)的矩形区域染成白色的最小代价,那么显然这个区域可以被按行或者按列分割成两个矩形来更新自身的值。显然需要使用区间dp(因为区间dp是以长度为阶段,普通dp是以起点为阶段,而更新过程需要使用不同起点的dp数组更新,如果使用普通dp会导致使用未更新到最优值的dp数组进行当前数组的更新,这显然不对),时间复杂度为O(n^5)
#include<bits/stdc++.h>
using namespace std;
//#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
const int maxn=55;
char ch[maxn][maxn];
int dp[maxn][maxn][maxn][maxn],sum[maxn][maxn];
int main() {
</