Hdu 2513 区间DP
Cake slicing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 149 Accepted Submission(s): 86Problem DescriptionA 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 partsFor 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.InputThe 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.OutputOutput an integer indicating the least total length of the cutting edges.Sample Input3 4 3
1 2
2 3
3 2Sample OutputCase 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的更多相关文章
- hdu 4283 区间dp
You Are the One Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化
HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...
- HDU 4293---Groups(区间DP)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=4293 Problem Description After the regional con ...
- String painter HDU - 2476 -区间DP
HDU - 2476 思路:分解问题,先考虑从一个空串染色成 B串的最小花费 ,区间DP可以解决这个问题 具体的就是,当 str [ l ] = = str [ r ]时 dp [ L ] [ R ] ...
- HDU 4632 区间DP 取模
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4632 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字 ...
- 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) ...
- HDU 4570(区间dp)
E - Multi-bit Trie Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- hdu 2476 区间dp
题意: 给出两个串s1和s2,一次只能将一个区间刷一次,问最少几次能让s1=s2 例如zzzzzfzzzzz,长度为11,我们就将下标看做0~10 先将0~10刷一次,变成aaaaaaaaaaa 1~ ...
- hdu 4632(区间dp)
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/ ...
- HDU 5273 区间DP
输入一组数,m次询问 问每一个询问区间的逆序数有多少 区间DP简单题 #include "stdio.h" #include "string.h" int dp ...
随机推荐
- neo4j 实战、实例、示例 创建电影关系图 -1
1. 创建关系 因为代码占篇幅太大,创建整个"电源关系图"的代码在文章最下方. 2. 简单分析创建语句 2.1 创建电影节点 CREATE (TheMatrix:Movie {ti ...
- 订单风险系统BI
最近被公司叫去协助传统做维表查询服务,项目已经做完.和前端联调过程发现oracle对查询 sql和产品设计还是挺重要的.不能全部堆给代码去做,如何方便代码,代码优化到最高性能才是首要解决的事,前端才能 ...
- windows server 文件夹和搜索选项被禁用了
当我们需要调整 windows 文件夹相关的配置时,却发现“文件夹和搜索选项”被禁用了,下图是恢复正常的情况.被禁用时显示灰色,不能点击. 下面给出解决步骤: 打开“组策略”. 然后依次展开“用户配置 ...
- HBase访问接口
- 廖雪峰Java11多线程编程-2线程同步-1同步代码块
1.线程安全问题 多个线程同时运行,线程调度由操作系统决定,程序本身无法决定 如果多个线程同时读写共享变量,就可能出现问题 class AddThread extends Thread{ public ...
- html常用标签详解4-列表标签
列表标签 列表标签分为3大类:无序列表.有序列表.自定义列表.线面我会依依简单介绍 一.无序列表 有个type属性: 默认:disc:实心小圆点:circle:空心小圆点:square:实心小方块:n ...
- 使用gRPC-Gateway快速构建微服务-双向认证下rpc-gateway使用(同时提供rpc和http接口)
https://github.com/grpc-ecosystem/grpc-gateway 在grpc之上加一层代理并转发,转变成protobuf格式来访问grpc服务 安装 go get -u g ...
- PAT甲级——A1024 Palindromic Number
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- java监控文件夹下的文件变化使用jnotify
https://blog.csdn.net/codepython/article/details/42341243?utm_source=blogxgwz1 使用jnotify https://blo ...
- hashMap 源码解读理解实现原理和hash冲突
hashMap 怎么说呢. 我的理解是 外表是一个set 数组,无序不重复 . 每个set元素是一个bean ,存着一对key value 看看代码吧 package test; import jav ...