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
题意:给你一个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;
}