UVa--1629(区间动规 / 记忆化搜索)
2014-08-25 14:48:59
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
思路:这题一开始我是直接记忆化搜索+枚举水过的,但看了别人的题解发现也可以递推,而且效率较高。
dp[x][y][h][w]表示以(x,y)为左下角坐标,且高为h,宽为w的蛋糕切完需要的最小长度和。
状态转移方程:dp[x][y][h][w] = min(dp[x][y][h][k1] + dp[x + k1][y][h][w - k1] + h(列切), dp[x][y][k2][w] + dp[x][y + k2][h - k2][w] + w(行切)),(1 <= k1 < w , 1 <= k2 < h),注意要切完后的两部分蛋糕中都有樱桃这一刀才能切。
先附上自己写的记忆化搜索版,1300+ms。
1 /************************************************************************* 2 > File Name: a.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Mon 25 Aug 2014 01:21:44 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 const int INF = 1 << 28; 16 17 int dp[25][25][25][25]; 18 int g[25][25]; 19 int cnt[25][25]; 20 int n,m,k; 21 int Case = 0; 22 23 int Solve(int x1,int y1,int x2,int y2){ 24 int &res = dp[x1][y1][x2][y2]; 25 if(res != -1) return res; 26 int tmp = cnt[x2][y2] - cnt[x1 - 1][y2] - cnt[x2][y1 - 1] + cnt[x1 - 1][y1- 1]; 27 if(tmp == 0) return INF; 28 if(tmp == 1) return 0; 29 res = INF; 30 for(int i = y1; i < y2; ++i) 31 res = min(res,Solve(x1,y1,x2,i) + Solve(x1,i + 1,x2,y2) + x2 - x1 + 1); 32 for(int i = x1; i < x2; ++i) 33 res = min(res,Solve(x1,y1,i,y2) + Solve(i + 1,y1,x2,y2) + y2 - y1 + 1); 34 return res; 35 } 36 37 int main(){ 38 int x,y; 39 while(scanf("%d%d%d",&n,&m,&k) == 3){ 40 memset(g,0,sizeof(g)); 41 while(k--){ 42 scanf("%d%d",&x,&y); 43 g[x][y] = 1; 44 } 45 memset(cnt,0,sizeof(cnt)); 46 for(int i = 1; i <= n; ++i) 47 for(int j = 1; j <= m; ++j) 48 cnt[i][j] = cnt[i - 1][j] + cnt[i][j - 1] - cnt[i - 1][j - 1] + g[i][j]; 49 memset(dp,-1,sizeof(dp)); 50 printf("Case %d: %d\n",++Case,Solve(1,1,n,m)); 51 } 52 return 0; 53 }
然后是改进后的递推版:(感觉比较难懂啊 QAQ,545ms,看到别人解题报告中有300+ms的,膜拜ing)
1 /************************************************************************* 2 > File Name: a.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Mon 25 Aug 2014 01:21:44 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 #define Fp(it,a,b) for(int it = a; it <= b; ++it) 16 #define Fm(it,a,b) for(int it = a; it >= b; --it) 17 const int INF = 1 << 29; 18 19 int dp[25][25][25][25]; 20 int g[25][25]; 21 int exi[25][25][25][25]; 22 int cnt[25][25]; 23 int n,m,k; 24 int Case = 0; 25 26 //以笛卡尔坐标系为参考 27 28 int main(){ 29 int tx,ty; 30 while(scanf("%d%d%d",&n,&m,&k) == 3){ 31 memset(g,0,sizeof(g)); 32 while(k--){ 33 scanf("%d%d",&tx,&ty); 34 g[ty][tx] = 1; 35 } 36 memset(cnt,0,sizeof(cnt)); 37 Fp(x,1,m) Fp(y,1,n) 38 cnt[x][y] = cnt[x - 1][y] + cnt[x][y - 1] - cnt[x - 1][y - 1] + g[x][y]; 39 40 Fp(w,1,m) Fp(h,1,n) Fp(x,1,m - w + 1) Fp(y,1,n - h + 1) 41 exi[x][y][h][w] = cnt[x + w - 1][y + h - 1] - cnt[x - 1][y + h - 1] - cnt[x + w - 1][y - 1] + cnt[x - 1][y - 1]; 42 43 int tmp; 44 Fp(w,1,m) Fp(h,1,n) Fp(x,1,m - w + 1) Fp(y,1,n - h + 1){ 45 int &ans = dp[x][y][h][w]; 46 if(exi[x][y][h][w] == 0) ans = INF; 47 else if(exi[x][y][h][w] == 1) ans = 0; 48 else{ 49 ans = INF; 50 Fp(k,1,w - 1) 51 if(exi[x][y][h][k] && exi[x + k][y][h][w - k]){ 52 tmp = dp[x][y][h][k] + dp[x + k][y][h][w - k] + h; 53 if(tmp < ans) ans = tmp; 54 } 55 Fp(k,1,h - 1) 56 if(exi[x][y][k][w] && exi[x][y + k][h - k][w]){ 57 tmp = dp[x][y][k][w] + dp[x][y + k][h - k][w] + w; 58 if(tmp < ans) ans = tmp; 59 } 60 } 61 } 62 printf("Case %d: %d\n",++Case,dp[1][1][n][m]); 63 } 64 return 0; 65 }

浙公网安备 33010602011771号