[CF1199 F. Rectangle Painting 1][区间dp]

这是一个关于在n×n网格中,将所有黑色单元格变为白色,以最小成本完成任务的问题。每一步操作是选择一个矩形并将其颜色改为白色,成本为矩形最大边长。通过区间动态规划方法,可以找到最小总成本。给出了一些示例和解释,并指出解决方案需要使用区间DP,避免了使用普通DP导致的未优化更新问题,时间复杂度为O(n^5)。
摘要由CSDN通过智能技术生成

传送门
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() {
   </
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值