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:

  1. /*************************************************************************
  2. > File Name: 2513.cpp
  3. > Author: Stomach_ache
  4. > Mail: sudaweitong@gmail.com
  5. > Created Time: 2014年07月10日 星期四 18时34分23秒
  6. > Propose:
  7. ************************************************************************/
  8.  
  9. #include <cmath>
  10. #include <string>
  11. #include <cstdio>
  12. #include <fstream>
  13. #include <cstring>
  14. #include <iostream>
  15. #include <algorithm>
  16. using namespace std;
  17.  
  18. #define min(x, y) ((x) < (y) ? (x) : (y))
  19.  
  20. int n, m, cherry;
  21. int dp[][][][];
  22. int a[][], sum[][];
  23.  
  24. int DP(int sx, int ex, int sy, int ey) {
  25. if (dp[sx][ex][sy][ey] != -) return dp[sx][ex][sy][ey];
  26. int cnt = ;
  27. for (int i = sx; i <= ex; i++) for (int j = sy; j <= ey; j++)
  28. if (a[i][j]) cnt++;
  29. if (cnt == ) return dp[sx][ex][sy][ey] = ;
  30.  
  31. int ans = 0x3f3f3f3f;
  32. for (int i = sx; i < ex; i++) {
  33. int tmp = sum[i][ey] - sum[i][sy-] - sum[sx-][ey] + sum[sx-][sy-];
  34. if (tmp) {
  35. ans = min(ans, DP(sx, i, sy, ey)+DP(i+, ex, sy, ey)+ey-sy+);
  36. }
  37. }
  38. for (int i = sy; i < ey; i++) {
  39. int tmp = sum[ex][i] - sum[ex][sy-] - sum[sx-][i] + sum[sx-][sy-];
  40. if (tmp) {
  41. ans = min(ans, DP(sx, ex, sy, i)+DP(sx, ex, i+, ey)+ex-sx+);
  42. }
  43. }
  44. return dp[sx][ex][sy][ey] = ans;
  45. }
  46.  
  47. int main(void) {
  48. int c = ;
  49. while(~scanf("%d %d %d", &n, &m, &cherry)) {
  50. memset(a, , sizeof(a));
  51. for (int i = ; i < cherry; i++) {
  52. int x, y;
  53. scanf("%d %d", &x, &y);
  54. a[x][y] = ;
  55. }
  56. memset(sum, , sizeof(sum));
  57. for (int i = ; i <= n; i++) {
  58. for (int j = ; j <= m; j++) {
  59. sum[i][j] = sum[i-][j] + sum[i][j-] - sum[i-][j-];
  60. if (a[i][j]) sum[i][j]++;
  61. }
  62. }
  63. memset(dp, -, sizeof(dp));
  64. DP(, n, , m);
  65. printf("Case %d: %d\n", c++, dp[][n][][m]);
  66. }
  67.  
  68. return ;
  69. }

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. neo4j 实战、实例、示例 创建电影关系图 -1

    1. 创建关系 因为代码占篇幅太大,创建整个"电源关系图"的代码在文章最下方. 2. 简单分析创建语句 2.1 创建电影节点 CREATE (TheMatrix:Movie {ti ...

  2. 订单风险系统BI

    最近被公司叫去协助传统做维表查询服务,项目已经做完.和前端联调过程发现oracle对查询 sql和产品设计还是挺重要的.不能全部堆给代码去做,如何方便代码,代码优化到最高性能才是首要解决的事,前端才能 ...

  3. windows server 文件夹和搜索选项被禁用了

    当我们需要调整 windows 文件夹相关的配置时,却发现“文件夹和搜索选项”被禁用了,下图是恢复正常的情况.被禁用时显示灰色,不能点击. 下面给出解决步骤: 打开“组策略”. 然后依次展开“用户配置 ...

  4. HBase访问接口

  5. 廖雪峰Java11多线程编程-2线程同步-1同步代码块

    1.线程安全问题 多个线程同时运行,线程调度由操作系统决定,程序本身无法决定 如果多个线程同时读写共享变量,就可能出现问题 class AddThread extends Thread{ public ...

  6. html常用标签详解4-列表标签

    列表标签 列表标签分为3大类:无序列表.有序列表.自定义列表.线面我会依依简单介绍 一.无序列表 有个type属性: 默认:disc:实心小圆点:circle:空心小圆点:square:实心小方块:n ...

  7. 使用gRPC-Gateway快速构建微服务-双向认证下rpc-gateway使用(同时提供rpc和http接口)

    https://github.com/grpc-ecosystem/grpc-gateway 在grpc之上加一层代理并转发,转变成protobuf格式来访问grpc服务 安装 go get -u g ...

  8. PAT甲级——A1024 Palindromic Number

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  9. java监控文件夹下的文件变化使用jnotify

    https://blog.csdn.net/codepython/article/details/42341243?utm_source=blogxgwz1 使用jnotify https://blo ...

  10. hashMap 源码解读理解实现原理和hash冲突

    hashMap 怎么说呢. 我的理解是 外表是一个set 数组,无序不重复 . 每个set元素是一个bean ,存着一对key value 看看代码吧 package test; import jav ...