Cake slicing

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

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

Accepted Code:

 /*************************************************************************
> File Name: 2513.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年07月10日 星期四 18时34分23秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define min(x, y) ((x) < (y) ? (x) : (y)) int n, m, cherry;
int dp[][][][];
int a[][], sum[][]; int DP(int sx, int ex, int sy, int ey) {
if (dp[sx][ex][sy][ey] != -) return dp[sx][ex][sy][ey];
int cnt = ;
for (int i = sx; i <= ex; i++) for (int j = sy; j <= ey; j++)
if (a[i][j]) cnt++;
if (cnt == ) return dp[sx][ex][sy][ey] = ; int ans = 0x3f3f3f3f;
for (int i = sx; i < ex; i++) {
int tmp = sum[i][ey] - sum[i][sy-] - sum[sx-][ey] + sum[sx-][sy-];
if (tmp) {
ans = min(ans, DP(sx, i, sy, ey)+DP(i+, ex, sy, ey)+ey-sy+);
}
}
for (int i = sy; i < ey; i++) {
int tmp = sum[ex][i] - sum[ex][sy-] - sum[sx-][i] + sum[sx-][sy-];
if (tmp) {
ans = min(ans, DP(sx, ex, sy, i)+DP(sx, ex, i+, ey)+ex-sx+);
}
}
return dp[sx][ex][sy][ey] = ans;
} int main(void) {
int c = ;
while(~scanf("%d %d %d", &n, &m, &cherry)) {
memset(a, , sizeof(a));
for (int i = ; i < cherry; i++) {
int x, y;
scanf("%d %d", &x, &y);
a[x][y] = ;
}
memset(sum, , sizeof(sum));
for (int i = ; i <= n; i++) {
for (int j = ; j <= m; j++) {
sum[i][j] = sum[i-][j] + sum[i][j-] - sum[i-][j-];
if (a[i][j]) sum[i][j]++;
}
}
memset(dp, -, sizeof(dp));
DP(, n, , m);
printf("Case %d: %d\n", c++, dp[][n][][m]);
} return ;
}

Hdu 2513 区间DP的更多相关文章

  1. hdu 4283 区间dp

    You Are the One Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

  3. HDU 4293---Groups(区间DP)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=4293 Problem Description After the regional con ...

  4. String painter HDU - 2476 -区间DP

    HDU - 2476 思路:分解问题,先考虑从一个空串染色成 B串的最小花费 ,区间DP可以解决这个问题 具体的就是,当 str [ l ] = = str [ r ]时 dp [ L ] [ R ] ...

  5. HDU 4632 区间DP 取模

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4632 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字 ...

  6. 2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp

    QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  7. HDU 4570(区间dp)

    E - Multi-bit Trie Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  8. hdu 2476 区间dp

    题意: 给出两个串s1和s2,一次只能将一个区间刷一次,问最少几次能让s1=s2 例如zzzzzfzzzzz,长度为11,我们就将下标看做0~10 先将0~10刷一次,变成aaaaaaaaaaa 1~ ...

  9. hdu 4632(区间dp)

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  10. HDU 5273 区间DP

    输入一组数,m次询问 问每一个询问区间的逆序数有多少 区间DP简单题 #include "stdio.h" #include "string.h" int dp ...

随机推荐

  1. Nginx是什么

    Nginx很强大,通常作为反向代理服务器,什么是反向代理服务器?就是客户端发送请求给Nginx ,Nginx收到请求后将请求转发给真正的服务器,然后接受服务器处理的结果,最后发送给客户端.客户端以为N ...

  2. 《DSP using MATLAB》Problem 8.15

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  3. 使用RequestsCookieJar自动保存并传递cookie

    使用python的requests开发爬虫程序的时候,经常需要将之前请求返回的cookie值作为下一个请求的cookie进行调用,比如模拟登录之后的返回的sessionID,就是需要作为后续请求的co ...

  4. 转载https://www.luogu.org/problemnew/solution/P1665,http://bailian.openjudge.cn/practice/2002/的新解法

    不知道为什么O(n^4)O(n4)的玄学方法能过,正解显然是O(n^2)O(n2)的,枚举对角线,然后算出另外两点判断存不存在. 关键就在怎么通过对角线算出另外两点的坐标. 先贴公式. int mid ...

  5. mongoDB可视化工具RoBo 3T的安装和使用

    第一步下载RoBo3T https://robomongo.org/download 第二步安装 双击安装包安装,修改安装路径,不停下一步,点击安装. 一路next,最后到了这个页面直接点击finis ...

  6. JZOJ100045 【NOIP2017提高A组模拟7.13】好数

    题目 题目大意 首先有一个定义: 对于一个数,如果和它互质的数可以组成一个等差数列,那么这个数叫"好数". 现在给你一个数列,有三种操作: 1.询问一段区间内的好数的个数. 2.将 ...

  7. 廖雪峰Java10加密与安全-3摘要算法-1MD5

    1.摘要算法 1.1 摘要算法(哈希算法/Hash/数字指纹): 计算任意长度数据的摘要(固定长度) 相同的输入数据始终得到相同的输出 不同的输入尽量得到不同的输出 1.2 摘要算法目的: 验证数据和 ...

  8. ROS urdf和xacro文件详解

    视觉标签:visual <visual> <origin xyz="0.0 0.0 0.0" /> <geometry> <box siz ...

  9. 工控安全入门(四)—— DNP3协议

    我们之前看过了法国施耐德的Modbus.德国西门子的S7comm,这次就让我们把目光投到美洲,看看加拿大的HARRIS的DNP3有什么特别之处. 这次选用的流量包部分来自w3h的gitbub: htt ...

  10. Http post请求案例

    public RmiRespBase sendHttpRes(String jsonParamStr, String url, String apiName,String systemId,Strin ...