题意:

n*m的方格中有k个点,现在要把方格分开使得每个点在一个部分,每分一次花费边长的费用,求完成花的最小费用

分析:

dp[sx][sy][ex][ey]表示分割起点(sx,sy)终点(ex,ey)的矩形最小花费,判断一下矩形内有无点,无点置成无穷大(不会被选择),若有一个点则完成分割值为0,若多于一个点继续分割。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int n,m,dp[][][][],used[][];
int judge(int sx,int sy,int ex,int ey){
int num=;
for(int i=sx+;i<=ex;++i)
for(int j=sy+;j<=ey;++j)
{
if(used[i][j]){
num++;
if(num==)return ;
}
}
return num;
}
int dfs(int sx,int sy,int ex,int ey){
if(dp[sx][sy][ex][ey]!=-)return dp[sx][sy][ex][ey];
if(judge(sx,sy,ex,ey)==)return dp[sx][sy][ex][ey]=INF;
if(judge(sx,sy,ex,ey)==)return dp[sx][sy][ex][ey]=;
int tmp=INF;
for(int i=sx+;i<ex;++i)
tmp=min(tmp,dfs(sx,sy,i,ey)+dfs(i,sy,ex,ey)+ey-sy);
for(int i=sy+;i<ey;++i)
tmp=min(tmp,dfs(sx,sy,ex,i)+dfs(sx,i,ex,ey)+ex-sx);
return dp[sx][sy][ex][ey]=tmp;
}
int main()
{
int t,ca=,k;
while(~scanf("%d%d%d",&n,&m,&k)){
int x,y;
memset(used,,sizeof(used));
for(int i=;i<k;++i)
{
scanf("%d%d",&x,&y);
used[x][y]=;
}
memset(dp,-,sizeof(dp));
printf("Case %d: %d\n",++ca,dfs(,,n,m));
}
return ;
}

Cake slicing的更多相关文章

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

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

  2. 1629 - Cake slicing(DP)

    花了近2个小时终于AC,好爽.. 一道类似于最优矩阵链乘的题目,受<切木棍>那道题的启示,该题的原理也是一样的,仅仅只是变成了且面积.那么对应的也要添加维度 . 显然要完整的表示状态,最少 ...

  3. uva1629,Cake Slicing,记忆化搜索

    同上个题一样,代码相似度极高,或者说可以直接用一个模板吧 dp[i,j,p,q]表示一块长为j-i+1,宽为q-p+1,左上角在位置(i,j)上的蛋糕,dp[]表示当前状态下的最优值,然后对该块蛋糕枚 ...

  4. UVA-1629 Cake slicing (DP、记忆化搜索)

    题目大意:一块n*m的矩形蛋糕,有k个草莓,现在要将蛋糕切开使每块蛋糕上都恰有一个(这意味着不能切出不含草莓的蛋糕块)草莓,要求只能水平切或竖直切,求最短的刀切长度. 题目分析:定义状态dp(xa,y ...

  5. UVA1629 Cake slicing

    题目传送门 直接暴力定义f[x1][y1][x2][y2]是使对角为\((x1, y1),(x2, y2)\)这个子矩形满足要求的最短切割线长度 因为转移顺序不好递推,采用记忆化搜索 #include ...

  6. UVa 1629 DP Cake slicing

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

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

  8. 【Uva 1629】 Cake slicing

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

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

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

随机推荐

  1. 在C#中调用另一个应用程序或命令行(.exe 带参数)<zz>

    在.net中使用system.diaglostics.Process可以用来调用另一个命令行或程序. using   System.Diagnostics;     如果是dos     Proces ...

  2. lintcode :Segmemt Tree Build II

    题目 Segmemt Tree Build II The structure of Segment Tree is a binary tree which each node has two attr ...

  3. php规范

    PSR-0 自动加载 PSR-1 基本代码规范 PSR-2 代码样式 PSR-3 日志接口

  4. MVC下基于DotNetOpenAuth 实现SSO单点登录

    具体官网可以查看:http://dotnetopenauth.net/,托管地址:https://github.com/DotNetOpenAuth/DotNetOpenAuth 可能需要FQ 博客园 ...

  5. Swift 版本很好的卡片切换效果基于ZLSwipeableView

    前言:在这篇文章你可以学到,一些基本的Swift语法, 基本UI控件闭包等. 实际的效果,比gif图的效果好很多. 卡片切换.gif 首先需要导入ZLSwipeableView pod 'ZLSwip ...

  6. PHP二位数组/多维数组 根据某个键值排序

    $arr[$i]['FirstName'] = $d_first_name;$arr[$i]['MiddleName'] = $d_middle_name;$arr[$i]['LastName'] = ...

  7. newClass a = Func(3)中隐藏的操作

    缘起 #include <iostream> #include <bitset> using namespace std; class A { public: A() { co ...

  8. 我的第一个jquery插件:下拉多选框

    <!DOCTYPE HTML> <html> <head> <title> New Document </title> <meta n ...

  9. RegexOne

    http://regexone.com/ http://regexone.com/lesson/optional_characters? http://regexone.com/lesson/capt ...

  10. css实现缩进无限嵌套

    使用css实现缩进带背景无限嵌套,支持Ie6,代码如下: <!DOCTYPE html> <html lang="en"> <head> < ...