1629 - Cake slicing

Time limit: 3.000 seconds

A rectangular cake with a grid of m * n <tex2html_verbatim_mark>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.

For example, assume that the cake has a grid of 3 * 4 <tex2html_verbatim_mark>unit squares on its top, and there are three cherries on the top, as shown in the figure below.

<tex2html_verbatim_mark>

One allowable slicing is as follows.

=6in <tex2html_verbatim_mark>

For this way of slicing, the total length of the cutting edges is 4+2=6.

Another way of slicing is

=6in <tex2html_verbatim_mark>

In this case, the total length of the cutting edges is 3+2=5.

Given 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.

Input

The input file contains multiple test cases. For each test case:

The first line contains three integers, n <tex2html_verbatim_mark>, m <tex2html_verbatim_mark>and k <tex2html_verbatim_mark>(1nm20) <tex2html_verbatim_mark>, where n * m <tex2html_verbatim_mark>is the size of the grid on the cake, and k <tex2html_verbatim_mark>is the number of the cherries.

Then k <tex2html_verbatim_mark>lines follow. Each line has two integers indicating the position 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.

Output

Output an integer indicating the least total length of the cutting edges.

Sample Input

3 4 3
1 2
2 3
3 2

Sample Output

Case 1: 5

比较经典的一个题目,记忆化搜索。
状态 d[u][d][l][r] 表示切上边为u,下边为d,左边为l,右边为r的矩形是的最少消耗。
一开始没有想到状态。
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define INF 1000000
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 21
int ma[MAXN][MAXN];
int n, m, k;
int p[MAXN][MAXN][MAXN][MAXN]; int Judge(int u, int d, int l, int r)
{
int t = ;
repu(i, u + , d + )
repu(j, l + , r + )
if(ma[i][j]) t++;
return t;
} int dp(int u, int d, int l, int r)
{
if(p[u][d][l][r] != -) return p[u][d][l][r];
int t = Judge(u, d, l, r);
if(t == ) return p[u][d][l][r] = ;
if(t == ) return p[u][d][l][r] = INF;
int minn = INF;
repu(i, u + , d) minn = min(minn, dp(u, i, l, r) + dp(i, d, l, r) + (r - l));
repu(i, l + , r) minn = min(minn, dp(u, d, i, r) + dp(u, d, l, i) + (d - u));
return p[u][d][l][r] = minn;
} int main()
{
int kase = ;
while(~scanf("%d%d%d", &n, &m, &k))
{
_cle(ma, );
_cle(p, -);
int x, y;
repu(i, , k) {
scanf("%d%d", &x, &y);
ma[x][y] = ;
}
printf("Case %d: %d\n", ++kase, dp(, n, , m));
}
return ;
}
 

uva 1629的更多相关文章

  1. 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 ...

  2. Uva 1629 切蛋糕

    题目链接:https://vjudge.net/contest/146179#problem/B 题意:一个矩形蛋糕上有好多个樱桃,现在要做的就是切割最少的距离,切出矩形形状的小蛋糕,让每个蛋糕上都有 ...

  3. UVa 1629 Cake slicing (记忆化搜索)

    题意:一个矩形蛋糕上有好多个樱桃,现在要做的就是切割最少的距离,切出矩形形状的小蛋糕,让每个蛋糕上都有一个樱桃,问最少切割距离是多少. 析:很容易知道是记忆化搜索,我们用dp[u][d][l][r]来 ...

  4. uva 1629切蛋糕(dp)

    有一个n行m列的网格蛋糕,上面有一些樱桃.求使得每块蛋糕上都有一个樱桃的分割最小长度 思路:dp. #include<cstdio> #include<cstring> #in ...

  5. UVa 1629 切蛋糕(记忆化搜索)

    https://vjudge.net/problem/UVA-1629 题意: 有一个n行m列的网格蛋糕上有一些樱桃.每次可以用一刀沿着网格线把蛋糕切成两块,并且只能直切不能拐弯.要求最后每一块蛋糕上 ...

  6. UVa 1629 DP Cake slicing

    题意: 一块n×m的蛋糕上有若干个樱桃,要求切割若干次以后,每块蛋糕上有且仅有1个樱桃.求最小的切割长度. 分析: d(u, d, l, r)表示切割矩形(u, d, l, r)所需要的最小切割长度. ...

  7. 【Uva 1629】 Cake slicing

    [Link]: [Description] 给你一个n*m的格子; 然后里面零零散散地放着葡萄 让你把它切成若干个小矩形方格 使得每个小矩形方格都恰好包含有一个葡萄. 要求切的长度最短; 问最短的切割 ...

  8. UVA - 1629 Cake slicing(切蛋糕)(dp---记忆化搜索)

    题意:有一个n行m列(1<=n, m<=20)的网格蛋糕上有一些樱桃.每次可以用一刀沿着网格线把蛋糕切成两块,并且只能够直切不能拐弯.要求最后每一块蛋糕上恰好有一个樱桃,且切割线总长度最小 ...

  9. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

随机推荐

  1. Object-c:两种文件读写的对比

    一.读写方法对比:(主要针对本地读取本地文件) 方式\操作 读 写 非URL方式 stringWithContentsOfFile writeToFile URL方式 stringWithConten ...

  2. YII的关联查询

    先看数据表中的关系是怎样的: customer表中的关系如下: order中的表为: 先在customer中获得order的数据,并与之关联,在 helloController.php 中,代码如下 ...

  3. SpringAop学习

    Spring Aop (jdk动态代理和cglib代理) Aop 的概念 aop即面向切面编程,一般解决具有横切面性质的体统(事务,缓存,安全) JDK动态代理: 可以使用实现proxy 类,实现jd ...

  4. misc设备

    WatchDog Timer驱动 混杂设备 Misc(或miscellaneous)驱动是一些拥有着共同特性的简单字符设备驱动.内核抽象出这些特性而形成一些API(在文件drivers/char/mi ...

  5. jQuery插件开发全解析

    jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...

  6. 【Todo】Python面试题分析记录(修饰器等)

    首先,看这一段代码: class A(object): x = 1 gen = (lambda t: (t for _ in xrange(10)))(x) if __name__ == '__mai ...

  7. Java 数据类型之间的转换 拆分字符串 Date/Calendar的转换

    数据类型转换 1. String - Int String str="123"; int i=1; int str=Integer.parseInt(str); String i= ...

  8. MyEclipse/Eclipse中XML文件的格式化配置

    Eclipse中XML文件的格式化配置 MyEclipse: 这一步的配置是使格式化的效果为控件的每个属性配置占一行.进入 Window/Preferences,展开到 XML/XML Resourc ...

  9. python 键值对的树实现

    #coding:utf-8 __author__ = 'similarface' class KeyedBinaryTree: def __init__(self):self.tree=EmptyNo ...

  10. easyui combobox 智能提示搜索

    <!-- 获取机会点名称列表 --><script> function initOpportunityNameFuzzyQuery() { $('#jihuidianmingc ...