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 ...
随机推荐
- matlab-变量类型-数组-矩阵
1 变量类型 1.1基本 1.2 特殊变量 ans •i,j: complex number •Inf: 无穷大 •eps: 2.2204e-016 •NaN: not a number •pi:pa ...
- 对CNN感受野一些理解
对CNN感受野一些理解 感受野(receptive field)被称作是CNN中最重要的概念之一.为什么要研究感受野呐?主要是因为在学习SSD,Faster RCNN框架时,其中prior box和A ...
- OpenGL学习笔记2017/8/29
OpenGL学习日志: 感谢doing5552 的OpenGL入门学习:http://www.cppblog.com/doing5552/archive/2009/01/08/71532.html 相 ...
- poj 1958
传送门 四塔汉诺塔问题,转移方程非常玄学,f[i]=min(f[j]*2+d[i-j]) (1 <=j < i),d表示三塔下的汉诺塔问题,这个方程的意思是将j个在四塔模式下有A挪到B,然 ...
- ArccGIS 10发布WFS服务并加载到Skyline中
下面用ArcGIS Server 10.0将建筑物图层发布为WFS服务. (1)创建mxd文件.ArcMap打开建筑物图层,存为Buildings.mxd文件.注意:必须统一空间参考系,且要与图层的坐 ...
- MyBatis配置文件(八)--databaseIdProvider数据库厂商标识
databaseIdProvider元素主要是为了支持不同厂商的数据库,比如有时候我们在公司内部开发使用的数据库都是PG(Postgresql),但是客户要求使用MySql,那就麻烦了是吧?其实在my ...
- MySQL-Utilities:mysqldiff
园子看到使用MySQL对比数据库表结构,参考测试发现 mysql> use test; create table test1 (id int not null primary key, a ) ...
- Python爬虫笔记【一】模拟用户访问之表单处理(3)
学习的课本为<python网络数据采集>,大部分代码来此此书. 大多数网页表单都是由一些HTML 字段.一个提交按钮.一个在表单处理完之后跳转的“执行结果”(表单属性action 的值)页 ...
- python-基础-面向对象2-异常-模块工厂模式
1 工厂模式和单例模式 1简单工厂模式 1.1.使用函数实现 # 定义伊兰特车类 class YilanteCar(object): # 定义车的方法 def move(self): print(&q ...
- jQuery 取值、赋值的基本方法整理
/*获得TEXT.AREATEXT的值*/ var textval = $("#text_id").attr("value"); //或者 var textva ...