Hdu 2513 Cake slicing【区间Dp】

Cake slicing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 252    Accepted Submission(s): 126


Problem Description
A rectangular cake with a grid of m*n unit squares on its top needs to be sliced into pieces. Several cherries are scattered on the top of the cake with at most one cherry on a unit square. The slicing should follow the rules below:
1.  each piece is rectangular or square;
2.  each cutting edge is straight and along a grid line;
3.  each piece has only one cherry on it;
4.  each cut must split the cake you currently cut two separate parts

For example, assume that the cake has a grid of 3*4 unit squares on its top, and there are three cherries on the top, as shown in the figure below.

One allowable slicing is as follows.

For this way of slicing , the total length of the cutting edges is 2+4=6.
Another way of slicing is 

In this case, the total length of the cutting edges is 3+2=5.

Give the shape of the cake and the scatter of the cherries , you are supposed to find
out the least total length of the cutting edges.
 

Input
The input file contains multiple test cases. For each test case:
The first line contains three integers , n, m and k (1≤n, m≤20), where n*m is the size of the unit square with a cherry on it . The two integers show respectively the row number and the column number of the unit square in the grid .
All integers in each line should be separated by blanks.
 

Output
Output an integer indicating the least total length of the cutting edges.
 

Sample Input
  
  
3 4 3 1 2 2 3 3 2
 

Sample Output
  
  
Case 1: 5

题目大意:

现在给你一个N*M的大蛋糕,上边一共有k个特殊物品,现在我们可以对一块蛋糕沿着格子线进行切割,现在希望将这个大蛋糕分成K块蛋糕,使得每一块蛋糕上边都有一个特殊物品,我们希望切割的长度最小,问这个最小长度。


思路:


求最小长度,又观察到数据范围,我们可以考虑暴力Dp去求解。

设定Dp【i】【j】【k】【l】表示现在蛋糕的剩余区间(矩形)为以(i,j)为左上角,(k,l)为右下角的情况的最小切割长度。


那么其状态转移方程也不难推出,分两种切割方法去暴力寻找切割点即可:




那么对于实现细节,我们可以直接记忆化搜索。


Ac代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int n,m,tot;
int a[25][25];
int dp[25][25][25][25];
int Dp(int i,int j,int k,int l)
{
    if(dp[i][j][k][l]==-1)
    {
        int cnt=0;
        for(int x=i;x<=k;x++)
        {
            for(int y=j;y<=l;y++)
            {
                if(a[x][y]==1)cnt++;
            }
        }
        if(cnt<=1)
        {
            dp[i][j][k][l]=0;
            return dp[i][j][k][l];
        }
        int minn=0x3f3f3f3f;
        for(int tmp=i;tmp<k;tmp++)
        {
            minn=min(minn,Dp(i,j,tmp,l)+Dp(tmp+1,j,k,l)+(l-j+1));
        }
        for(int tmp=j;tmp<l;tmp++)
        {
            minn=min(minn,Dp(i,j,k,tmp)+Dp(i,tmp+1,k,l)+(k-i+1));
        }
        dp[i][j][k][l]=minn;
        return dp[i][j][k][l];
    }
    else return dp[i][j][k][l];
}
int main()
{
    int kase=0;
    while(~scanf("%d%d%d",&n,&m,&tot))
    {
        memset(dp,-1,sizeof(dp));
        memset(a,0,sizeof(a));
        for(int i=0;i<tot;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            a[x][y]=1;
        }
        Dp(1,1,n,m);
        printf("Case %d: ",++kase);
        printf("%d\n",dp[1][1][n][m]);
    }
}







评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值