题意:

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. SDUT2142数据结构实验之图论二:基于邻接表的广度优先搜索遍历

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2142&cid=1186 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜 ...

  2. TDD三个阶段

    TDD的三个阶段 红灯.绿灯.重构 :明确了实施TDD所要遵循的工作流 (需求--->测试-->代码[重构]) 红灯阶段:      为不存在的代码编写测试 绿灯阶段:      仅编写适 ...

  3. http://www.oschina.net/translate/elasticsearch-getting-started?cmp

    http://www.oschina.net/translate/elasticsearch-getting-started?cmp

  4. 【Linux常识篇(1)】所谓的正向代理与反向代理

    正向代理的概念 正向代理,也就是传说中的代理,他的工作原理就像一个跳板,简单的说,我是一个用户,我访问不了某网站,但是我能访问一个代理服务器,这个代理服务器呢,他能访问那个我不能访问的网站,于是我先连 ...

  5. 【Linux高频命令专题(8)】五大查询命令

    find 格式 find 路径 -命令参数 [输出形式] 路径:告诉find在哪儿去找你要的东西 命令参数:参考下面 输出形式:输出形式很多,-print,-printf,-print,-exec,- ...

  6. 2014--9=17 软工二班 MyEclipse blue==1

    package cn.rwkj.test; import java.io.IOException; import java.net.ServerSocket; import java.net.Sock ...

  7. ubuntu 折腾之路

    aptitude search :search for the lib...and their realtions. apt-get install :install the app apt-get ...

  8. 各种分区类型对应的partition_Id

    ID Name Note == ==== ==== 00h empty [空] 01h DOS 12-bit FAT [MS DOS FAT12] 02h XENIX root file system ...

  9. Checked&Unchecked Exception

    Java 中定义了两类异常: 1) Checked exception: 这类异常都是Exception的子类 .异常的向上抛出机制进行处理,如果子类可能产生A异常,那么在父类中也必须throws A ...

  10. Android中View转换为Bitmap及getDrawingCache=null的解决方法

    1.前言 Android中经常会遇到把View转换为Bitmap的情形,比如,对整个屏幕视图进行截屏并生成图片:Coverflow中需要把一页一 页的view转换为Bitmap.以便实现复杂的图形效果 ...