• Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索


    E. Chocolate Bar

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/598/problem/E

    Description

    You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar.

    In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length.

    For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 32 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 22 = 4).

    For several given values nm and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - ksquares are not necessarily form a single rectangular piece.

    Input

    The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values nm and k to process.

    Each of the next t lines contains three integers nm and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively.

    Output

    For each nm and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly ksquares.

    Sample Input

    4
    2 2 1
    2 2 3
    2 2 2
    2 2 4

    Sample Output

    5
    5
    4
    0

    HINT

    题意

    给你n*m的巧克力,然后你想吃面积恰好为k的巧克力,问你怎么样才能花费最少吃到他

    每次花费是你切的这刀的长度的平方

    题解:

    记忆化搜索,然后中间直接暴力dfs就好了

    代码

    #include<iostream>
    #include<stdio.h>
    #include<cstring>
    using namespace std;
    int dp[50][50][50];
    
    int dfs(int n,int m,int k)
    {
        if(!k||n*m==k)return 0;
        if(dp[n][m][k]!=-1)return dp[n][m][k];
        int& minn = dp[n][m][k];
        minn = 9999999LL;
        for(int i=1;i<n;i++)
            for(int j=0;j<=k;j++)
                minn = min(minn,dfs(i,m,j)+dfs(n-i,m,k-j)+m*m);
        for(int i=1;i<m;i++)
            for(int j=0;j<=k;j++)
                minn = min(minn,dfs(n,i,j)+dfs(n,m-i,k-j)+n*n);
        return minn;
    }
    int main()
    {
        int t;scanf("%d",&t);
        memset(dp,-1,sizeof(dp));
        while(t--)
        {
            int n,m,k;
            scanf("%d%d%d",&n,&m,&k);
            printf("%d
    ",dfs(n,m,k));
        }
    
    }
  • 相关阅读:
    怎样写C代码——《狂人C》习题解答1——余响
    怎样写C代码——《狂人C》习题解答1(第一章习题7)
    充分理解表达式——《狂人C》习题解答2(第二章习题5)
    指针——《狂人C》观点
    欧拉项目练习题吧: 题目002
    JS调用XML绑定select[原创]
    SWFUpload在FF下的Bug[原创]
    winform实现嵌套类访问父类的控件[原创]
    在c#中如何动态改变iframe的src值,动态指向一个网页
    GridView里面模板获取唯一值(如ID)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4966148.html
Copyright © 2020-2023  润新知