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.
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.
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.
3 4 3 1 2 2 3 3 2
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;
}