HDU2513(记忆化搜索)

本文探讨了一个有趣的算法问题:如何将带有樱桃标记的矩形蛋糕,按照特定规则切成多个包含单一樱桃的矩形部分,同时最小化切割总长度。通过动态规划方法,我们实现了对任意大小和樱桃分布的蛋糕进行最优切割的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cake slicing

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 544 Accepted Submission(s): 282

Problem Description
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[x1][y1][x2][y2]dp[x1][y1][x2][y2]dp[x1][y1][x2][y2]表示矩形(左上角(x1,y1),右下角(x2,y2))的切割线长度。

#include<iostream>
#include<cstring>
using namespace std;

#define inf 0x3f3f3f3f

int n,m,k;
int a[25][25];
int dp[25][25][25][25];

int dfs(int x1,int y1,int x2,int y2) { //(x1,y1)到(x2,y2)
	int ans=dp[x1][y1][x2][y2];
	if(ans!=-1) return ans;
	int cnt=0;
	for(int i=x1; i<=x2; i++) {
		for(int j=y1; j<=y2; j++) {
			if(a[i][j]==1) {
				cnt++;
			}
		}
	}
	if(cnt==0) {
		dp[x1][y1][x2][y2]=inf;
		return inf;
	}else if(cnt==1){
		dp[x1][y1][x2][y2]=0;
		return 0;
	}
	
	ans=inf;
	for(int i=x1;i<x2;i++){
		ans=min(ans,dfs(x1,y1,i,y2)+dfs(i+1,y1,x2,y2)+y2-y1+1); 
	}
	for(int i=y1;i<y2;i++){
		ans=min(ans,dfs(x1,y1,x2,i)+dfs(x1,i+1,x2,y2)+x2-x1+1); 
	}
	dp[x1][y1][x2][y2]=ans;
	return ans;
}

int main() {
	int x,y;
	int cas=0;
	while(cin>>n>>m>>k) {
		cas++;
		memset(a,0,sizeof(a));
		for(int i=0; i<k; i++) {
			cin>>x>>y;
			a[x][y]=1;
		}
		memset(dp,-1,sizeof(dp));
		cout<<"Case "<<cas<<": "<<dfs(1,1,n,m)<<endl;
	}


	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值