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 ...
随机推荐
- Vuejs之Component slot 插槽详解
Vuejs的component的数据进行了沙箱隔离,除js全局变量如Math, Date之类外无法访问用户自定义的变量,所以使用component写组件或嵌套组件时明白变量的访问非常重要 编译作用域 ...
- 2019-8-31-dotnet-手动解决-json-解析中不合法字符串
title author date CreateTime categories dotnet 手动解决 json 解析中不合法字符串 lindexi 2019-08-31 16:55:58 +0800 ...
- Codeforces 442B. Andrey and Problem
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- MacOS 读写 NTFS 即插即用.
1. 安装osxfusehttps://osxfuse.github.io/ 2. 安装brewhttps://brew.sh/index_zh-cn.html 3. 安装ntfs-3gbrew in ...
- json-server 和mock.js生成大量json数据
JSON-server和mock.jsmock文件夹下 db.json db.jsjson-sever使用 安装:npm install json-server -g/mock 目录下执行json-s ...
- 微信小程序发送手机验证码---倒计时
var currentTime = 59 //倒计时的事件(单位:s)var interval = null //倒计时函数 Page({ data: { time:59 //倒计时 }, onLoa ...
- Java 普通代码块,构造代码块,静态代码块
具体百度... 这里只记下,构造构造代码块在构造函数之前执行,每创建一个对象则执行一次. 静态代码块属于类,而不是对象,只执行一次.
- Web充斥着存在漏洞的过期JavaScript库
虽然使用第三方软件库通常会降低开发的时间,但同时也会增加网站暴露出的攻击表面,对此我们应有充分的认识.因此需要保持第三方软件库的最新版本依赖,以便从安全更新中获益.即便如此,一份近期研究表明,在Ale ...
- 《数据结构与算法分析——C语言描述》ADT实现(NO.01) : 栈(Stack)
这次的数据结构是一种特殊的线性表:栈(Stack) 栈的特点是后入先出(LIFO),可见的只有栈顶的一个元素. 栈在程序中的地位非常重要,其中最重要的应用就是函数的调用.每次函数调用时都会创建该函数的 ...
- ubuntu16.04环境编译gSOAP
一.gSOAP简介 SOAP 是基于 XML 的简易协议,可使应用程序在 HTTP 之上进行信息交换.或者更简单地说:SOAP 是用于访问网络服务的协议. SOAP 提供了一种标准的方法,使得运行在 ...