题目传送门

直接暴力定义f[x1][y1][x2][y2]是使对角为\((x1, y1),(x2, y2)\)这个子矩形满足要求的最短切割线长度

因为转移顺序不好递推,采用记忆化搜索

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;
LL read() {LL k = 0, f = 1; char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9')k = k * 10 + c - 48, c = getchar();return k * f;
}
int f[21][21][21][21], sum[21][21];
bool mapp[21][21];
int dp(int x1, int y1, int x2, int y2) {int num = sum[x2][y2] - sum[x1-1][y2] - sum[x2][y1-1] + sum[x1-1][y1-1];if(num == 1) return f[x1][y1][x2][y2] = 0;if(num == 0) return f[x1][y1][x2][y2] = 2147483647 / 3;if(f[x1][y1][x2][y2] != -1) return f[x1][y1][x2][y2];f[x1][y1][x2][y2] = 2147483647 / 3;for(int i = x1; i < x2; ++i)f[x1][y1][x2][y2] = min(dp(i+1, y1, x2, y2) + dp(x1, y1, i, y2) + abs(y1 - y2) + 1, f[x1][y1][x2][y2]);for(int i = y1; i < y2; ++i)f[x1][y1][x2][y2] = min(dp(x1, y1, x2, i) + dp(x1, i+1, x2, y2) + abs(x1 - x2) + 1, f[x1][y1][x2][y2]);return f[x1][y1][x2][y2];
}
int n, m, k;
void solve(int tot) {memset(f, -1, sizeof(f));memset(mapp, 0, sizeof(mapp));for(int i = 1; i <= k; ++i) {int x = read(), y = read();mapp[x][y] = 1;}for(int i = 1; i <= n; ++i)for(int j = 1; j <= m; ++j)sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1] + mapp[i][j];dp(1, 1, n, m);printf("Case %d: %d\n", tot, f[1][1][n][m]);
}
int main() {int tot = 0;while(scanf("%d %d %d", &n, &m, &k) != EOF)solve(++tot);return 0;
}

转载于:https://www.cnblogs.com/wxl-Ezio/p/11028787.html

UVA1629 Cake slicing相关推荐

  1. 切蛋糕 Cake slicing

    UVA1629 定义: U p p e r L e f t UpperLeft UpperLeft为矩形左上角, L o w e r R i g h t LowerRight LowerRight为右 ...

  2. uva 1629——Cake slicing

    题意:有一个n行m列网格蛋糕上有一些樱桃,每次可以把蛋糕一刀切成两块,最后使每块蛋糕上都有一个樱桃且切割线总长度最小. 思路:区间dp,记忆化搜索整个过程,然后分区间从上到下,从左到右,分区间递归找到 ...

  3. 暑训day1解题报告

    A - Painting the sticks 因为不能覆盖涂/涂两次,所以就数数有几个三个一块儿就行了. #include<cstdio> int a[100],ans ; int ma ...

  4. 区间DP小结(附经典例题)

    写这篇文章的目的主要是想总结下区间DP的经典题目,同时给自己复习巩固这方面知识点. 区间DP 一.定义 ​ 区间DP是线性动态规划的扩展,适用场景为每段区间的最优解可以通过更小区间的最优解得到.所以我 ...

  5. 紫书《算法竞赛入门经典》

    紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...

  6. 【日程训练】算法脱贫计划

    算法脱贫计划 前言 2020-12-26 2020-12-27 2020-12-28 ~ 2020-1-1 2021-1-1 ~ 2021-1-16 [寒假训练计划]2021-1-17 ~ 2021- ...

  7. [Cake] 1. CI中的Cake

    在上一篇C#Make自动化构建-简介中,简单的介绍了下Cake的脚本如何编写以及通过Powershell或者Bash在本地运行Cake脚本.本篇在此基础上,介绍下如何在CI环境中使用Cake. 1. ...

  8. 人群场景分析--Slicing Convolutional Neural Network for Crowd Video Understanding

    Slicing Convolutional Neural Network for Crowd Video Understanding CVPR2016 http://www.ee.cuhk.edu.h ...

  9. Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp

    D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...

最新文章

  1. 【阿里聚安全·安全周刊】Intel芯片级安全漏洞事件|macOS存在漏洞
  2. linux truncate文件,linux系统编程:用truncate调整文件大小
  3. c++——优先队列(priority_queue)
  4. python36 mysql_python-day36(初识MySQL数据库)
  5. 读书:梭罗的《瓦尔登湖》
  6. 如何用VsCode进行Debug
  7. 设计模式之路 | 外观模式
  8. 【javascript】字符串逆序输出
  9. 我的世界服务器怎么变无线耐久,我的世界大型服务器三小时新手变老手技巧
  10. ajax status code 415,Ajax向后台传json格式的数据出现415错误的原因分析及解决方法...
  11. 【语音增强】基于matlab小波变换语音增强【含Matlab源码 296期】
  12. 电脑键盘部分按键失灵_笔记本键盘个别按键失灵怎么处理win10
  13. 如何打开电脑端口——以开启mysql数据库3306端口为例
  14. 浙江理工大学本科毕业答辩beamer模板
  15. 2022华为杯E题思路分析
  16. 灰度拉伸python_对比度拉伸(一些基本的灰度变换函数)基本原理及Python实现
  17. 解决由于ntoskrnl.exe导致的蓝屏
  18. Java_计算机基础知识
  19. apache c语言源码,Apache模块开发/用C语言扩展apache(2:APR编程介绍)
  20. 当linux reboot不成功时怎么办

热门文章

  1. Java Graphics2D类的绘图方法
  2. JZOJ 5454. 【NOIP2017提高A组冲刺11.5】仔细的检查
  3. php sql漏洞修复,php sql注入漏洞与修复
  4. resultset rs =pst.executequery();发生异常_07795.14.4HMaster无法成为Active异常分析
  5. python import出错_Python ImportError: cannot import name urlopen错误分析
  6. poj3261(求至少出现k次的可重叠的子串的长度)
  7. AtCoder AGC002F Leftmost Ball (DP、组合计数)
  8. c语言扫雷游戏代码_C语言游戏详解---扫雷游戏
  9. arrays中copyof复制两个数组_异或的魅力!图解「数组中两个数的最大异或值」
  10. numpy 数组抽取_Python 关于NumPy的用法介绍