uva1629Cake slicing

来源:互联网 发布:战舰世界 数据更新出错 编辑:程序博客网 时间:2024/05/20 02:27

题目描述:
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
这里写图片描述
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.
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 on the right corner.
One allowable slicing is as follows.
For this way of slicing, the total length of the cutting edges is 4+2=6.
Another way of slicing is
这里写图片描述
In this case, the total length of the cutting edges is 3+2=5.
Given 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
grid on the cake, and k is the number of the cherries.
Then k lines follow. Each line has two integers indicating the position 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,记忆化搜索,使用数组d[x1][x2][y1][y2]记录所在的长方形蛋糕的最小切割长度,分别在x方向和y方向更新数组。

ac代码:

#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>using namespace std;const int maxn=25;const int inf=0x3f3f3f3f;int map[maxn][maxn],cnt[maxn][maxn],d[maxn][maxn][maxn][maxn];int dp(int x1,int x2,int y1,int y2){    int &ans=d[x1][x2][y1][y2];    if(ans>=0)    return ans;    int tmp=cnt[x2][y2]-cnt[x1-1][y2]-cnt[x2][y1-1]+cnt[x1-1][y1-1];    if(tmp==0)                                      //不合法的切法,返回INF是为了不让这种切法更新进答案     return ans=inf;    if(tmp==1)    return ans=0;    ans=inf;    for(int i=x1;i<x2;i++)    ans=min(dp(x1,i,y1,y2)+dp(i+1,x2,y1,y2)+y2-y1+1,ans);    for(int i=y1;i<y2;i++)    ans=min(dp(x1,x2,y1,i)+dp(x1,x2,i+1,y2)+x2-x1+1,ans);    return ans;}int main(){    //freopen("in.txt","r",stdin);    int n,m,k;    int cas=0;    while(~scanf("%d%d%d",&n,&m,&k))    {        memset(map,0,sizeof(map));        for(int i=0;i<k;i++)        {            int x,y;            scanf("%d%d",&x,&y);            map[x][y]=1;        }        memset(cnt,0,sizeof(cnt));        for(int i=1;i<=n;i++)          for(int j=1;j<=m;j++)            cnt[i][j]=cnt[i-1][j]+cnt[i][j-1]+map[i][j]-cnt[i-1][j-1];  //预处理,计算所在长方形内的樱桃数         memset(d,-1,sizeof(d));        int ans=dp(1,n,1,m);        printf("Case %d: %d\n",++cas,ans);    }}
0 0