UVA 1629 Cake slicing(四维DP)【分割格子类模板】

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

  【题解】这是一道矩阵分割的模板题,可以作为这一类动态规划题的模板,思路就是用一个四维数组dp[a][b][c][d]来表示矩阵(a,b)(c,d),其中a,b是矩阵左上角,c,d是右下角,此矩阵表示当前矩阵的最小花费,判断一下矩形内有无点,无点置成无穷大(不会被选择),若有一个点则完成分割值为0,若多于一个点继续分割,递归下去就好。


 【AC代码】

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=25;
const int INF=1e9+7;
int m,n,k;
int node[N][N];
int dp[N][N][N][N];

int min(int x,int y)
{
    return x>y?y:x;
}

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(node[i][j]){
                num++;
            if(num==2)
                return 2;
            }
        }
    }
    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 ss=INF;
    for(int i=sx+1;i<ex;++i)
    {
        ss=min(ss,dfs(i,sy,ex,ey)+dfs(sx,sy,i,ey)+ey-sy);
    }

    for(int i=sy+1;i<ey;++i)
    {
        ss=min(ss,dfs(sx,i,ex,ey)+dfs(sx,sy,ex,i)+ex-sx);
    }
    return dp[sx][sy][ex][ey]=ss;
}

int main()
{
    int cnt=1;
    while(~scanf("%d%d%d",&m,&n,&k))
    {
        int a,b;
        memset(node,0,sizeof(node));
        for(int i=0;i<k;i++)
        {
            scanf("%d%d",&a,&b);
            node[a][b]=1;
        }
        memset(dp,-1,sizeof(dp));
        printf("Case %d: %d\n",cnt++,dfs(0,0,m,n));
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值