Educational Codeforces Round 1 E. Chocolate Bar dp

题目链接:http://codeforces.com/contest/598/problem/E

E. Chocolate Bar
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
4
2 2 1
2 2 3
2 2 2
2 2 4
output
5
5
4
0
Note

In the first query of the sample one needs to perform two breaks:

  • to split 2 × 2 bar into two pieces of 2 × 1 (cost is 22 = 4),
  • to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 12 = 1).

In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.

题意:给你一块n*m的巧克力,取出k单位面积最小需要花费的价值;

思路:%q的代码

   一直不知道该怎么写,然后看他dfs,记忆话搜索,果然好写多了;

   dp,dp[i][j][k]表示当前i行j列大小的巧克力,取出大小为k的最小花费;

   枚举切割的行或者列,两个不同面,所取的面积大小;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e7+2,M=1e7+10,inf=1e9+10;
const ll INF=1e18+10,mod=1e9+7;
///   数组大小
int dp[50][50][60];
int dfs(int n,int m,int k)
{
    if(k>n*m)return inf;
    if(dp[n][m][k]<inf)return dp[n][m][k];
    if(k==0||k==n*m)return dp[n][m][k]=0;
    for(int i=1;i<=n/2;i++)
    {
        for(int j=0;j<=(i*m,k);j++)
        {
            int x=dfs(n-i,m,j);
            int y=dfs(i,m,k-j);
            dp[n][m][k]=min(dp[n][m][k],x+y+m*m);
        }
    }
    for(int i=1;i<=m/2;i++)
    {
        for(int j=0;j<=(i*n,k);j++)
        {
            int x=dfs(n,m-i,j);
            int y=dfs(n,i,k-j);
            dp[n][m][k]=min(dp[n][m][k],x+y+n*n);
        }
    }
    return dp[n][m][k];
}
int main()
{
    for(int i=0;i<=30;i++)
    {
        for(int j=0;j<=30;j++)
        {
            for(int k=0;k<=min(i*j,50);k++)
                dp[i][j][k]=inf;
        }
    }
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        printf("%d\n",dfs(n,m,k));
    }
    return 0;
}

 

posted @ 2017-04-06 19:26  jhz033  阅读(212)  评论(0编辑  收藏  举报