zoukankan      html  css  js  c++  java
  • UVa 1629 Cake slicing (记忆化搜索)

    题意:一个矩形蛋糕上有好多个樱桃,现在要做的就是切割最少的距离,切出矩形形状的小蛋糕,让每个蛋糕上都有一个樱桃,问最少切割距离是多少。

    析:很容易知道是记忆化搜索,我们用dp[u][d][l][r]来表示,上界是u,下界是d,左边是l,右边是r,然后不断切割,不过要注意切的时候是按缝隙切,

    缝隙多一条,那么我们可以补上一条,用0来补齐,然后就进行计算就好。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <stack>
    using namespace std;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const LL LNF = 100000000000000000;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 20 + 5;
    const int mod = 1e9 + 7;
    const char *mark = "+-*";
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    int n, m;
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    inline LL Max(LL a, LL b){  return a < b ? b : a; }
    inline LL Min(LL a, LL b){  return a > b ? b : a; }
    inline int Max(int a, int b){  return a < b ? b : a; }
    inline int Min(int a, int b){  return a > b ? b : a; }
    bool cake[maxn][maxn];
    int dp[maxn][maxn][maxn][maxn];
    
    int cal(int u, int d, int l, int r){
        int cnt = 0;
        for(int i = u+1; i <= d; ++i)
            for(int j = l+1; j <= r; ++j){
                if(cake[i][j]) ++cnt;
                if(cnt >= 2)  return 2;
            }
        return cnt;
    }
    
    int DP(int u, int d, int l, int r){
        int &ans = dp[u][d][l][r];
        if(ans >= 0)  return ans;
        int num = cal(u, d, l, r);
        if(1 == num)  return ans = 0;
        if(!num)   return ans = INF;
        ans = INF;
        for(int i = u+1; i < d; ++i)
            ans = Min(ans, DP(u, i, l, r) + DP(i, d, l, r) + r - l);
        for(int i = l+1; i < r; ++i)
            ans = Min(ans, DP(u, d, l, i) + DP(u, d, i, r) + d - u);
        return ans;
    }
    
    int main(){
        int k, kase = 0;
        while(scanf("%d %d %d", &n, &m, &k) == 3){
            memset(cake, false, sizeof cake);
            for(int i = 0; i < k; ++i){
                int x, y;
                scanf("%d %d", &x, &y);
                cake[x][y] = true;
            }
            memset(dp, -1, sizeof dp);
            printf("Case %d: %d
    ", ++kase, DP(0, n, 0, m));
        }
        return 0;
    }
    
  • 相关阅读:
    网络流24题之 1738: 最小路径覆盖问题
    POJ 1966 Cable TV Network
    网络流24题- 魔术球问题
    网络流24题之 圆桌问题
    可持久化线段树维护启发式合并的可持久化并查集
    HDU 6166 Senior Pan
    ACM对拍cpp程序
    双联通分量复习
    欧拉回路求路径POJ 2230
    LCA的两种求法
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5793449.html
Copyright © 2011-2022 走看看