Uva 1629 切蛋糕

时间:2024-04-29 14:14:18

题目链接:https://vjudge.net/contest/146179#problem/B

题意:一个矩形蛋糕上有好多个樱桃,现在要做的就是切割最少的距离,切出矩形形状的小蛋糕,让每个蛋糕上都有一个樱桃~问最少切割距离是?

分析:可以根据每次的切割范围遍历找最优值,也就是说状态描述d[u][d][l][r] 是这个范围内的最短距离。要是这个范围内只有一个樱桃,那么就是 0 ,要是没有樱桃就是无穷大。

 #include <bits/stdc++.h>

 using namespace std;

 const int INF = 0x3f3f3f3f;

 bool maps[][];
int r,c,cnt; int dps[][][][]; int sum(int u,int d,int l,int r)
{
int sums = ;
for(int i=u; i<=d; i++)
for(int j=l; j<=r; j++)
if(maps[i][j])
sums++;
return sums;
} int dp(int u,int d,int l,int r)
{
int &ans = dps[u][d][l][r];
if(ans!=-)
return ans;
else if(sum(u,d,l,r)==)
return ans=;
else if(sum(u,d,l,r)==)
return ans=INF;
else
{
ans = INF;
for(int i=u; i<=d; i++)
ans = min(ans,dp(u,i,l,r)+dp(i+,d,l,r)+r-l+);
for(int i=l; i<=r; i++)
ans = min(ans,dp(u,d,l,i)+dp(u,d,i+,r)+d-u+);
}
return ans;
} int main()
{
int kase = ;
while(scanf("%d%d%d",&r,&c,&cnt)!=EOF)
{
memset(dps,-,sizeof(dps));
memset(maps,,sizeof(maps)); for(int i=; i<cnt; i++)
{
int rx,ry;
scanf("%d%d",&rx,&ry);
maps[rx][ry] = ;
} printf("Case %d: %d\n",kase++,dp(,r,,c));
} return ;
}