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.
<tex2html_verbatim_mark>One allowable slicing is as follows.
<tex2html_verbatim_mark>For this way of slicing, the total length of the cutting edges is 4+2=6.
Another way of slicing is
<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>(1
n, 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
随机推荐
- [SAP ABAP开发技术总结]屏幕跳转
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- JS——时间日期控件
原文:http://blog.sina.com.cn/s/blog_621768f30100qmfz.html 今天找到一个还不错的日历控件 下载地址:http://www.my97.net/dp/d ...
- WPF GroupBox样式
来源:http://code.msdn.microsoft.com/WPF-GroupBox-Style-1d9df7c5/ 效果: XAML CODE: <Window xmlns=" ...
- lotus 公式
函数 描述@UserName 返回用户名或服务器名.@Name([key]; name) 更改用户名的格式.关键字包含 [CN] 以从一个专有名字中解析出公共名,[Abbreviate] 缩写规范格式 ...
- NYOJ214
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长 ...
- iOS - Delegate 代理
1.Delegate 1.1 协议 协议:是多个类共享的一个方法列表.协议中列出的方法没有相应的实现,计划由其他人来实现.协议中列出的方法,有些是可以选择实现,有些是必须实现. 1>.如果你定义 ...
- adb_亮屏
来自:http://justonlypiano.blogspot.tw/2013/08/adbandroidandroid-phone-screen-off-via.html 更多内容请查看:http ...
- matplotlib库的常用知识
看看matplotlib是什么? matplotlib是python上的一个2D绘图库,它可以在夸平台上边出很多高质量的图像.综旨就是让简单的事变得更简单,让复杂的事变得可能.我们可以用matplot ...
- @synchronized (object)使用详解
synchronized关键字代表这个方法加锁,相当于不管哪一个线 程A每次运行到这个法时,都要检查有没有其它正在用这个方法的线程B(或者C D等),有的话要等正在使用这个方法的线程B(或者C D)运 ...
- TCP/IP协议学习(一) LWIP实现网络远程IAP下载更新
最近需要实现通过TCP/IP远程IAP在线更新功能,忙了2周终于在原有嵌入式服务器的基础上实现了该功能,这里就记录下实现的过程. IAP又称在应用编程,其实说简单点就是实现不需要jlink,仅通过芯片 ...