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. HDU 3923 Invoker | 暑训Day1 C题填坑

    暑训第一天,专题为组合数学与概率期望. 最近一个月都没有学习新的知识,上午听聚聚讲课头脑都是一片空白.加上长期没刷题,下午做练习题毫无感觉.到晚上总算理清了蓝书上的一些概念,跟着榜单做题.最后唯独剩下 ...

  2. PAT甲级——A1098 Insertion or Heap Sort

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  3. netty优化参考链接

    Netty百万级推送服务设计要点:http://www.infoq.com/cn/articles/netty-million-level-push-service-design-points/ 用N ...

  4. <数据库>MySQL的安装及安装中存在的问题

    无脑三连: 下载:https://dev.mysql.com/downloads/mysql/5.7.html#downloads 解压:任意目录 添加环境变量:WIN10步骤 我的电脑→属性→高级系 ...

  5. 08-2-if的其他写法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. drools跳转出现错误问题(400)

    400 Sorry, a technical error occurred. Please contact a system administrator. 今天drools的管理平台tomcat部署完 ...

  7. Cmp- Linux必学的60个命令

    1.作用 cmp(“compare”的缩写)命令用来简要指出两个文件是否存在差异,它的使用权限是所有用户. 2.格式 cmp[options] 文件名 3.[options]主要参数 -l: 将字节以 ...

  8. [转]【全面解禁!真正的Expression Blend实战开发技巧】第八章 FluidMoveBehavior完全解析之一漂浮移动

    好久没更新博客了,今天如果没急事,准备连发三篇,完全讲解Blend最牛的元素-“FluidMoveBehavior”.我向大家保证这三章一定非常精彩,不看你肯定后悔.我相信这三篇文章发表后,国内很多s ...

  9. php用正则表达式匹配URL的简单方法(亲测可行)

    https://www.jb51.net/article/43093.htm 在PHP的官网上看到的parse_url()函数的替代方案.结果和parse_url()函数差不多,是使用正则实现的.UR ...

  10. Spring注解驱动开发(四)-----aop、声明式事务

    AOP 概念 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式:-----基于动态代理 一个aop示例 1.导入aop模块:Spring AOP:(spring-aspects ...