uva 1629切蛋糕(dp)
有一个n行m列的网格蛋糕,上面有一些樱桃。求使得每块蛋糕上都有一个樱桃的分割最小长度
思路:dp。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#define eps 1e-6
#define LL long long
using namespace std; const int maxn = 100 + 5;
const int INF = 10000000;
int d[22][22][22][22];
int n, m, K, kase = 0;//樱桃数量
int cherry[22][22], sumv[22][22]; int cpu(int a, int b, int c, int d) {
return sumv[b][d] - sumv[a-1][d] - sumv[b][c-1] + sumv[a-1][c-1];
} int dp(int begx, int endx, int begy, int endy, int cherrynum) {
int& ans = d[begx][endx][begy][endy];
if(cherrynum == 1) return 0;
if(ans != -1) return ans;
ans = INF;
for(int i = begx; i < endx; i++) if(cpu(begx, i, begy, endy) > 0 && cpu(begx, i, begy, endy) < cherrynum)
ans = min(ans, dp(begx, i, begy, endy, cpu(begx, i, begy, endy))+dp(i+1, endx, begy, endy, cherrynum-cpu(begx, i, begy, endy))+endy-begy+1);
for(int i = begy; i < endy; i++) if(cpu(begx, endx, begy, i) > 0 && cpu(begx, endx, begy, i) < cherrynum)
ans = min(ans, dp(begx, endx, begy, i, cpu(begx, endx, begy, i))+dp(begx, endx, i+1, endy, cherrynum-cpu(begx, endx, begy, i))+endx-begx+1);
return ans;
} void init() {
memset(d, -1, sizeof(d));
memset(cherry, 0, sizeof(cherry));
memset(sumv, 0, sizeof(sumv));
int x, y;
for(int i = 0; i < K; i++) {
cin >> x >> y;
cherry[x][y] = 1;
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
sumv[i][j] = sumv[i][j-1] + sumv[i-1][j] + cherry[i][j] - sumv[i-1][j-1];
} void solve() {
printf("Case %d: %d\n", ++kase, dp(1, n, 1, m, K));
} int main() {
//freopen("input.txt", "r", stdin);
while(scanf("%d%d%d", &n, &m, &K) == 3) {
init();
solve();
}
return 0;
}
uva 1629切蛋糕(dp)的更多相关文章
- Uva 1629 切蛋糕
题目链接:https://vjudge.net/contest/146179#problem/B 题意:一个矩形蛋糕上有好多个樱桃,现在要做的就是切割最少的距离,切出矩形形状的小蛋糕,让每个蛋糕上都有 ...
- UVa 1629 切蛋糕(记忆化搜索)
https://vjudge.net/problem/UVA-1629 题意: 有一个n行m列的网格蛋糕上有一些樱桃.每次可以用一刀沿着网格线把蛋糕切成两块,并且只能直切不能拐弯.要求最后每一块蛋糕上 ...
- P1714 切蛋糕 dp+单调队列
题意: 题目描述 在幻想乡,琪露诺是以笨蛋闻名的冰之妖精. 某一天,琪露诺又在玩速冻青蛙,就是用冰把青蛙瞬间冻起来.但是这只青蛙比以往的要聪明许多,在琪露诺来之前就已经跑到了河的对岸.于是琪露诺决定到 ...
- Cake slicing UVA - 1629
UVA - 1629 ans[t][b][l][r]表示t到b行,l到r列那一块蛋糕切好的最小值d[t][b][l][r]表示t到b行,l到r列区域的樱桃数,需要预处理 #include<cst ...
- openjudge6047分蛋糕[DP]
描述 有一块矩形大蛋糕,长和宽分别是整数w .h.现要将其切成m块小蛋糕,每个小蛋糕都必须是矩形.且长和宽均为整数.切蛋糕时,每次切一块蛋糕,将其分成两个矩形蛋糕.请计算:最后得到的m块小蛋糕中,最大 ...
- LRJ入门经典-0903切蛋糕305
原题 LRJ入门经典-0903切蛋糕305 难度级别:B: 运行时间限制:1000ms: 运行空间限制:256000KB: 代码长度限制:2000000B 试题描述 如图所示有一个矩形蛋糕,上面划分成 ...
- 洛谷P1528 切蛋糕 [搜索,二分答案]
题目传送门 切蛋糕 题目描述 Facer今天买了n块蛋糕,不料被信息组中球球等好吃懒做的家伙发现了,没办法,只好浪费一点来填他们的嘴巴.他答应给每个人留一口,然后量了量每个人口的大小.Facer有把刀 ...
- UVA.674 Coin Change (DP 完全背包)
UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...
- [洛谷P1528] 切蛋糕
洛谷题目链接:切蛋糕 题目描述 Facer今天买了n块蛋糕,不料被信息组中球球等好吃懒做的家伙发现了,没办法,只好浪费一点来填他们的嘴巴.他答应给每个人留一口,然后量了量每个人口的大小.Facer有把 ...
随机推荐
- android 手机拍照返回 Intent==null 以及intent.getData==null
手机拍照第一种情况:private void takePicture(){ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);Si ...
- 红包外挂史及AccessibilityService分析与防御
最近在做一个有趣的外挂的小玩意,前提我们要了解一个重要的类AccessibilityService 转载请注明出处:https://lizhaoxuan.github.io 前言 提起Accessib ...
- Flex 布局教程转载
Flex 布局教程:语法篇 http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html Flex 布局教程:实例篇 http://www.ruan ...
- ADO.NET的五大对象【转】
ADO.NET是一组用于和数据源进行交换的面向对象类库.通常情况下,数据源是数据库,也可以是文本文件.Excel表或XML文件.ADO.NET的主要类对象有Connection.Command.Dat ...
- Promise in Chakra
http://www.ecma-international.org/ecma-262/#sec-fulfillpromise 25.4.1.3.1 and 25.4.1.3.2 Promise Rej ...
- DDD领域模型系统的工作流(十四)
在自定义的Windows窗体中运行工作流:(把工作流的代码放入到文本框中) public partial class Form1 : Form { public Form1() { Initializ ...
- hdu 1757 和1005差不多 (矩阵快速幂)
If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-1 ...
- DbCommandInterceptor抓取EF执行时的SQL语句
EF6.1也出来不少日子了,6.1相比6.0有个很大的特点就是新增了System.Data.Entity.Infrastructure.Interception 命名空间,此命名空间下的对象可以允许我 ...
- 【noip模拟赛7】足球比赛 树
描述 在2009的中国城市足球比赛中,在2^N支队中,有一些队在开赛前宣布了退出比赛.比赛采取的是淘汰赛.比如有4支队伍参加,那么1队和2队比赛,3队和4队赛,然后1队和2队的胜者与3队和4队的胜者争 ...
- 在storm中运行jar产生模拟数据的时候,遇见的问题
1.问题由来 命令:java -jar data.jar 1000 >>nginx.log 报错: Exception in thread "main" java.la ...