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

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

传送门
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() {
   
   
    int n;<
提供的引用中未提及解决“class java.awt.geom.Rectangle2D$Double cannot be cast to class java.awt.Rectangle (java.awt.geom.Rectangle2D$Double and java.awt.Rectangle are in module java.desktop of loader 'bootstrap')”错误的方法。不过,通常这种类型转换错误是因为尝试将一个对象强制转换为不兼容的类型。以下是一些可能的解决思路: ### 检查代码逻辑 确保代码中没有错误地尝试将 `java.awt.geom.Rectangle2D.Double` 对象强制转换为 `java.awt.Rectangle` 对象。这两个类虽然都与矩形有关,但它们是不同的类,不能直接进行强制类型转换。 ### 使用适当的转换方法 如果需要从 `Rectangle2D.Double` 获取 `Rectangle` 的等效信息,可以通过提取 `Rectangle2D.Double` 的属性来创建一个新的 `Rectangle` 对象。示例代码如下: ```java import java.awt.Rectangle; import java.awt.geom.Rectangle2D; public class RectangleConversion { public static void main(String[] args) { Rectangle2D.Double rect2D = new Rectangle2D.Double(10, 20, 30, 40); // 创建一个新的 Rectangle 对象 Rectangle rect = new Rectangle( (int) rect2D.getX(), (int) rect2D.getY(), (int) rect2D.getWidth(), (int) rect2D.getHeight() ); System.out.println("Rectangle: " + rect); } } ``` ### 类型检查 在进行类型转换之前,使用 `instanceof` 关键字检查对象的类型,避免不必要的类型转换错误。示例代码如下: ```java import java.awt.Rectangle; import java.awt.geom.Rectangle2D; public class TypeCheckExample { public static void main(String[] args) { Rectangle2D.Double rect2D = new Rectangle2D.Double(10, 20, 30, 40); if (rect2D instanceof Rectangle) { Rectangle rect = (Rectangle) rect2D; System.out.println("Converted to Rectangle: " + rect); } else { System.out.println("Cannot convert Rectangle2D.Double to Rectangle."); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值