Cake slicing
A rectangular cake with a grid of MXN unit squares in its top
Needs to is sliced into pieces. Several Cherries is 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 was straight and along a grid line;
3. Each piece have only one cherry on it.
For example, assume the cake have a grid of 3x4 unit squares on its top, and there is three
Cherries on the top, as shown in the "gure on the right corner.
One allowable slicing is as follows.
For this by slicing, the total length of the cutting edges is 4+2=6.
Another slicing is
The the total length of the cutting edges is 3+2=5.
Given the shape of the cake and the scatter of the cherries, you is supposed to? nd out the least
Total length of the cutting edges.
Input
The input Le contains multiple test cases. For each test case:
The. RST line contains three integers, N, M and K (1≤n, m≤20), where NXM is the size of the
Grid on the cake, and K are the number of the cherries.
then k lines follow. Integers indicating the position of the unit square with a
Cherry on it. The integers show respectively the row number and the column number of the unit
Square in the grid.
All integers and should are 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
Test instructions
Give you a grid of n rows m columns with some cakes on it. Each time you can use a knife to cut along the grid line two pieces, and only can be straight cut not turning.
Asking for a cherry on every last piece of cake,
And the total length of the cutting line is minimal.
Exercises
We'll just have to remember the flowers.
Set Dp[row][col][x][y] at the current point (x, y) is the top left corner of the cake block the minimum number of cuts, and ensure that the number of blocks as many as possible
We'll just enumerate through that place.
#include <bits/stdc++.h>using namespacestd;Const intN = -, M = -, INF = 1e9+7, mod = 1e9+7;intT,n,m,k,mp[n][n],dp[n][n][n][n],vis[n][n][n][n];intDfsintRowintColintXinty) {if(Vis[row][col][x][y])returnDp[row][col][x][y]; int& ret =Dp[row][col][x][y]; RET=inf; Vis[row][col][x][y]=1; if(row==1&&col==1) {ret= Mp[x][y]?0: INF; returnret; } intCNT =0; for(inti=x;i<=row+x-1; i++) for(intj=y;j<=y+col-1; j + +)if(Mp[i][j]) cnt++; if(CNT = =1) {ret=0; returnret; } if(CNT = =0)returnret; for(inti=y+1; i<=y+col-1; i++) {ret= Min (Ret,dfs (row,i-y,x,y) +dfs (row,col-i+y,x,i) +row); } for(inti=x+1; i<=x+row-1; i++) {ret= Min (Ret,dfs (i-x,col,x,y) +col+dfs (row-i+x,col,i,y)); } returnret;}intMain () {intX,y,cas =1; while(SCANF ("%d%d%d", &n,&m,&k)! =EOF) {memset (MP,0,sizeof(MP)); memset (dp,-1,sizeof(DP)); memset (Vis,0,sizeof(VIS)); for(intI=1; i<=k;i++) scanf ("%d%d", &x,&y), mp[x][y] =1; printf ("Case %d:%d\n", Cas++,dfs (N,m,1,1)); } return 0;}
UVA 1629 Cake Slicing memory search