动态规划——Cake slicing
来源:互联网 发布:基因调控网络仿真 编辑:程序博客网 时间:2024/06/15 10:00
A rectangular cake with a grid of m*n 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;
4. each cut must split the cake you currently cut two separate parts
For example, assume that the cake has a grid of 3*4 unit squares on its top, and there are three cherries on the top, as shown in the figure below.
![](https://odzkskevi.qnssl.com/79e06861e45184b2c9cc654c30a99139?v=1500256601)
One allowable slicing is as follows.
![](https://odzkskevi.qnssl.com/3ffeb5a8ad73eb78fb83e79e086251d7?v=1500256601)
For this way of slicing , the total length of the cutting edges is 2+4=6.
Another way of slicing is
![](https://odzkskevi.qnssl.com/db000a40cd2184c524536939a661d1b1?v=1500256601)
In this case, the total length of the cutting edges is 3+2=5.
Give 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.
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;
4. each cut must split the cake you currently cut two separate parts
For example, assume that the cake has a grid of 3*4 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 2+4=6.
Another way of slicing is
In this case, the total length of the cutting edges is 3+2=5.
Give 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.
The first line contains three integers , n, m and k (1≤n, m≤20), where n*m is the size 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.
3 4 31 22 33 2
Case 1: 5
题意:给你一个n×m的网格,然后有k个樱桃的坐标,让你去切成几个矩形方块,要求每个方块上面都有个樱桃,求最短的切割距离。
思路:
记忆化搜索,用dp[sx][sy][ex][ey]表示左上角为(sx,sy)和右下角为(ex, ey)的这块矩形上所需的最短距离。
若这块矩形上无点就表示无穷大,有一个点就表示为0,多余一个点就继续分割
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;const int maxn=30;int n, m , k;int dp[maxn][maxn][maxn][maxn];int pl[maxn][maxn];int check(int sx, int sy, int ex, int ey) //判断矩形上有无点{ int num=0; for(int i=sx+1; i<=ex; i++) for(int j=sy+1; j<=ey; j++) if(pl[i][j]) { num++; if(num==2) return num; } return num;}int dfs(int sx, int sy, int ex, int ey){ if(dp[sx][sy][ex][ey]!=-1) return dp[sx][sy][ex][ey]; if(check(sx, sy, ex,ey)==0) return dp[sx][sy][ex][ey]=INF; if(check(sx, sy, ex, ey)==1) return dp[sx][sy][ex][ey]=0; int ans=INF; for(int i=sy+1; i<ey; i++) //竖着割 ans=min(ans, dfs(sx, i, ex, ey)+dfs(sx, sy, ex, i)+ex-sx); for(int i=sx+1; i<ex; i++) //横着割 ans=min(ans, dfs(i, sy, ex, ey)+dfs(sx, sy, i, ey)+ey-sy); return dp[sx][sy][ex][ey]=ans;}int main(){ int cas=1; while(~scanf("%d%d%d", &m, &n, &k)) { int x, y; memset(pl, 0, sizeof(pl)); memset(dp, -1, sizeof(dp)); for(int i=0; i<k; i++) //将樱桃标记 { scanf("%d%d", &x, &y); pl[x][y]=1; } printf("Case %d: %d\n",cas++,dfs(0,0,m,n)); } return 0;}
阅读全文
0 0
- 动态规划——Cake slicing
- 100道动态规划——6 UVA 1629 Cake slicing 记忆化搜索
- UVA1629[Cake slicing] 棋盘动态规划
- uva 1629——Cake slicing
- UVA 1629 Cake slicing——记忆化搜索
- UVa 1629 - Cake slicing
- UVA1629---Cake slicing
- 1629 - Cake slicing
- Uva1629:Cake slicing
- UVA 1629 Cake slicing
- UVa1629 Cake Slicing
- Cake slicing UVA
- 【UVA1629】Cake slicing
- Cake slicing UVA
- codeforces589B Layer Cake (动态规划)
- 1629 - Cake slicing(DP)
- UVA 1629 - Cake slicing 题解
- UVa 1629 Cake Slicing DP
- VPS搭建与IPv6使用教程
- 二叉树中两个节点的最近公共父节点
- 二叉树的基本操作
- vueRouter 2.0路由配置
- jzoj5234. 【NOIP2017模拟8.7A组】外星人的路径
- 动态规划——Cake slicing
- Scrapy爬取图片资源
- redis原理
- caffe随记(六)---tools工具举例
- Mac上用什么清理工具最好?
- Summer Training day4上帝与集合的正确用法 欧拉函数+降幂公式
- HDU 4771 Stealing Harry Potter's Precious
- C与C++的相同点与不同点
- 视图