动态规划——Cake slicing

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个樱桃的坐标,让你去切成几个矩形方块,要求每个方块上面都有个樱桃,求最短的切割距离。

思路:

记忆化搜索,用dp[sx][sy][ex][ey]表示左上角为(sx,sy)和右下角为(ex, ey)的这块矩形上所需的最短距离。

若这块矩形上无点就表示无穷大,有一个点就表示为0,多余一个点就继续分割


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=30;
int n, m , k;
int dp[maxn][maxn][maxn][maxn];
int pl[maxn][maxn];
int check(int sx, int sy, int ex, int ey)    //判断矩形上有无点
{
    int num=0;
    for(int i=sx+1; i<=ex; i++)
        for(int j=sy+1; j<=ey; j++)
            if(pl[i][j])
            {
                num++;
                if(num==2)
                    return num;
            }
    return num;
}
int dfs(int sx, int sy, int ex, int ey)
{
    if(dp[sx][sy][ex][ey]!=-1)
        return dp[sx][sy][ex][ey];
    if(check(sx, sy, ex,ey)==0)
        return dp[sx][sy][ex][ey]=INF;
    if(check(sx, sy, ex, ey)==1)
        return dp[sx][sy][ex][ey]=0;
    int ans=INF;
    for(int i=sy+1; i<ey; i++)    //竖着割
        ans=min(ans, dfs(sx, i, ex, ey)+dfs(sx, sy, ex, i)+ex-sx);
    for(int i=sx+1; i<ex; i++)    //横着割
        ans=min(ans, dfs(i, sy, ex, ey)+dfs(sx, sy, i, ey)+ey-sy);
    return dp[sx][sy][ex][ey]=ans;
}
int main()
{
    int cas=1;
    while(~scanf("%d%d%d", &m, &n, &k))
    {
        int x, y;
        memset(pl, 0, sizeof(pl));
        memset(dp, -1, sizeof(dp));
        for(int i=0; i<k; i++)    //将樱桃标记
        {
            scanf("%d%d", &x, &y);
            pl[x][y]=1;
        }
        printf("Case %d: %d\n",cas++,dfs(0,0,m,n));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值