uva 1629
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:
- each piece is rectangular or square;
- each cutting edge is straight and along a grid line;
- 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.

One allowable slicing is as follows.

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

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>(1n, m
20) <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的更多相关文章
- 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 ...
- Uva 1629 切蛋糕
题目链接:https://vjudge.net/contest/146179#problem/B 题意:一个矩形蛋糕上有好多个樱桃,现在要做的就是切割最少的距离,切出矩形形状的小蛋糕,让每个蛋糕上都有 ...
- UVa 1629 Cake slicing (记忆化搜索)
题意:一个矩形蛋糕上有好多个樱桃,现在要做的就是切割最少的距离,切出矩形形状的小蛋糕,让每个蛋糕上都有一个樱桃,问最少切割距离是多少. 析:很容易知道是记忆化搜索,我们用dp[u][d][l][r]来 ...
- uva 1629切蛋糕(dp)
有一个n行m列的网格蛋糕,上面有一些樱桃.求使得每块蛋糕上都有一个樱桃的分割最小长度 思路:dp. #include<cstdio> #include<cstring> #in ...
- UVa 1629 切蛋糕(记忆化搜索)
https://vjudge.net/problem/UVA-1629 题意: 有一个n行m列的网格蛋糕上有一些樱桃.每次可以用一刀沿着网格线把蛋糕切成两块,并且只能直切不能拐弯.要求最后每一块蛋糕上 ...
- UVa 1629 DP Cake slicing
题意: 一块n×m的蛋糕上有若干个樱桃,要求切割若干次以后,每块蛋糕上有且仅有1个樱桃.求最小的切割长度. 分析: d(u, d, l, r)表示切割矩形(u, d, l, r)所需要的最小切割长度. ...
- 【Uva 1629】 Cake slicing
[Link]: [Description] 给你一个n*m的格子; 然后里面零零散散地放着葡萄 让你把它切成若干个小矩形方格 使得每个小矩形方格都恰好包含有一个葡萄. 要求切的长度最短; 问最短的切割 ...
- UVA - 1629 Cake slicing(切蛋糕)(dp---记忆化搜索)
题意:有一个n行m列(1<=n, m<=20)的网格蛋糕上有一些樱桃.每次可以用一刀沿着网格线把蛋糕切成两块,并且只能够直切不能拐弯.要求最后每一块蛋糕上恰好有一个樱桃,且切割线总长度最小 ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
随机推荐
- EditPlus添加到右键菜单
1.Alt+R 键打开“运行” 2.“运行”中输入:regedit 打开注册表 (1.在 "我的电脑HKEY_CLASSES_ROOT*" 下新建项 shell: (2.在 ...
- C# 线程(五):线程池
From : http://kb.cnblogs.com/page/42531/ 在多线程的程序中,经常会出现两种情况: 一种情况: 应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然 ...
- 转载java源代码阅读方法
刚才在论坛不经意间,看到有关源码阅读的),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 说到技术基础,我打个比方吧,如果你从来没有学过Java,或是任何一门编程语言 ...
- 转 。。。。一个不规则的按钮 虽然已经不适用于cocos2dx3.0以上版本 but思路就应该是这个样子滴
本篇文章主要讲一下怎么做一个不规则的按钮,比如如下图的八卦,点击绿色和点击红色部分,需要执行不同的事件
- Data Science 和 Finance 两个领域的融合是什么样子的?
Data Science 和 Finance 两个领域的融合是什么样子的? 2015-05-24 董可人 现在大部分人所说的Quant一般是指各大投行里做衍生品定价,信用评估,风险控制之类工作的人,这 ...
- mysql delimiter
默认情况下,mysql遇到分号; 就认为是一个命令的终止符, 就会执行命令.而有些时候,我们不希望这样,比如存储过程中包含多个语句,这些语句以分号分割,我们希望这些语句作为一个命令,一起执行,怎么解决 ...
- linux清空文件等有用的指令
1). > filename 2). :> filename 3). echo "" > filename (文件大小被截为1字节) 4). ...
- Hadoop与Spark比较
先看这篇文章:http://www.huochai.mobi/p/d/3967708/?share_tid=86bc0ba46c64&fmid=0 直接比较Hadoop和Spark有难度,因为 ...
- Android LayoutInflater 动态地添加删除View
我想实现点击一个按钮(或其他的事件)添加或删除View,网上找到了LayoutInflater这个类. 下面是我自己一些经验: android官网上LayoutInflater的API:http:// ...
- Openfire 是怎么存离线消息
原文:http://myopenfire.com/article/getarticle/26 1.openfire默认怎么存离线消息 在默认情况下,不添加任何插件的情况下,当用户不在线,对于发送给 ...