Codeforces Round #576 (Div. 1)-D. Rectangle Painting 1

14 篇文章 0 订阅
9 篇文章 0 订阅

D. Rectangle Painting 1

题意: 给定一个 n × n n\times n n×n的矩阵, 其中有黑有白, 先要将所有的点全变白, 且已知将矩阵 x × y x\times y x×y变白的代价是 m a x ( x , y ) max(x, y) max(x,y),现问将整块矩阵变白的最小花费

>> face <<

Strategy: 二维前缀和 + 记忆化搜索dp

一开始我还以为可以用普通dp[i][j]找递推, 结果没成功, 看到别人都是用记搜做的, 然后我也试了一发, 然后发现搜索的时候要优化没全白的区域, 这个是个典型的棋盘DP, 第一次没有想到是很正常的

solution:

#include <bits/stdc++.h>
using namespace std;

#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rev(i, a, b) for (int i = (a); i >= (b); --i)
#define _for(i, a, b) for (int i = (a); i < (b); ++i)
#define _rof(i, a, b) for (int i = (a); i > (b); --i)
#define oo 0x3f3f3f3f
#define ll long long
#define db double
#define eps 1e-8
#define bin(x) cout << bitset<10>(x) << endl;
#define what_is(x) cerr << #x << " is " << x << "s" << endl;
#define met(a, b) memset(a, b, sizeof(a))
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
const int maxn = 50 + 10;
char mat[maxn][maxn];
int dp[maxn][maxn][maxn][maxn], sum[maxn][maxn];
inline int quary(int x1, int y1, int x2, int y2)
{
	return sum[y2][x2] - sum[y1 - 1][x2] - sum[y2][x1 - 1] + sum[y1 - 1][x1 - 1];
}
inline int calu(int x1, int y1, int x2, int y2)
{
	int& t = dp[x1][y1][x2][y2];
	if (~t)
		return t;
	
	t = min(quary(x1, y1, x2, y2), max(x2 - x1 + 1, y2 - y1 + 1));
	if(t == 0)return t;
	_for(i, x1, x2)
		t = min(t, calu(x1, y1, i, y2) + calu(i + 1, y1, x2, y2));
	_for(i, y1, y2)
		t = min(t, calu(x1, y1, x2, i) + calu(x1, i + 1, x2, y2));
	return t;
}
signed main()
{
	//ios::sync_with_stdio(0);
	int n;
	cin >> n;
	_rep(i, 1, n)
	{
		scanf("%s", mat[i] + 1);
		_rep(j, 1, n)
		{
			if (mat[i][j] == '#')
				sum[i][j]++;
		}
	}
	_rep(i, 1, n)
	{
		_rep(j, 1, n)
		{
			sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + sum[i][j];
		}
	}
	met(dp, -1);
	cout << calu(1, 1, n, n) << endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值