【cf1199F】F. Rectangle Painting 1(二维区间dp)

传送门

题意:
给出一张大小为(ncdot n,nleq 50)的网格,现有一些点为黑点,有一些点为白点。
现在可以执行若干次操作:选定一个大小为(wcdot h)(自选)的矩形,将矩形中的所有点都变为白点,消耗代价为(max(w,h))
问最少需要多少代价,使得最终网格中所有点为白点。

思路:

  • 考虑(dp),对于一个矩形范围来说,其代价为(max(w,h)),若要使答案更优,那么就得减少(max)
  • 假设现在考虑(dp[x_1][y_1][x_2][y_2]),若(x_2-x_1>y_2-y_1),那么就考虑将这个矩形横着划分为两块,通过上下两个矩形合并来得到答案。
  • 此时我们枚举行,若在矩形范围内存在一行为空行,此时合并则可能会使得答案更优。

这题和常规的网格中的(dp)稍微有点不同,有点类似于二维区间(dp)的感觉。想到将一个矩形横竖进行划分得到最优解基本这个题就出来了。

/*
 * Author:  heyuhhh
 * Created Time:  2020/3/13 11:56:12
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << '
'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 50 + 5;

int n;
char s[N][N];
int sum[N][N], dp[N][N][N][N];

void run() {
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> (s[i] + 1);
        for(int j = 1; j <= n; j++) {
            sum[i][j] = (s[i][j] == '#');
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            sum[i][j] += sum[i][j - 1];
        }   
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            sum[i][j] += sum[i - 1][j];
        }   
    }   
    auto query = [&](int r1, int c1, int r2, int c2) {
        return sum[r2][c2] + sum[r1 - 1][c1 - 1] - sum[r2][c1 - 1] - sum[r1 - 1][c2];
    };
    for(int x1 = 1; x1 <= n; x1++) {
        for(int y1 = 1; y1 <= n; y1++) {
            for(int x2 = x1; x2 <= n; x2++) {
                for(int y2 = y1; y2 <= n; y2++) {
                    dp[x1][y1][x2][y2] = max(x2 - x1 + 1, y2 - y1 + 1);
                    if(x1 == x2 && y1 == y2) dp[x1][y1][x2][y2] = (s[x1][y1] == '#');
                }   
            }
        }   
    }
    for(int k1 = 1; k1 <= n; k1++) {
        for(int k2 = 1; k2 <= n; k2++) {
            for(int x1 = 1; x1 <= n; x1++) {
                for(int y1 = 1; y1 <= n; y1++) {
                    int x2 = x1 + k1 - 1, y2 = y1 + k2 - 1;
                    if(x2 > n || y2 > n) break;
                    int& now = dp[x1][y1][x2][y2];
                    if(k1 >= k2) {
                        for(int t = x1; t <= x2; t++) {
                            if(query(t, y1, t, y2) == 0) {
                                now = min(now, dp[x1][y1][t - 1][y2] + dp[t + 1][y1][x2][y2]);
                            }
                        }
                    } else {
                        for(int t = y1; t <= y2; t++) {
                            if(query(x1, t, x2, t) == 0) {
                                now = min(now, dp[x1][y1][x2][t - 1] + dp[x1][t + 1][x2][y2]);   
                            }
                        }
                    }
                }   
            }
        }   
    }
    cout << dp[1][1][n][n] << '
';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run(); 
    return 0;
}

原文地址:https://www.cnblogs.com/heyuhhh/p/12507164.html