uva 1629

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:

  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.

For example, assume that the cake has a grid of * 4 <tex2html_verbatim_mark>unit squares on its top, and there are three cherries on the top, as shown in the figure below.

epsfbox{p3946a.eps}<tex2html_verbatim_mark>

One allowable slicing is as follows.

=6in epsfbox{p3946b.eps}<tex2html_verbatim_mark>

For this way of slicing, the total length of the cutting edges is 4+2=6.

Another way of slicing is

=6in epsfbox{p3946c.eps}<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$ le$nm$ le$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

比较经典的一个题目,记忆化搜索。
状态 d[u][d][l][r] 表示切上边为u,下边为d,左边为l,右边为r的矩形是的最少消耗。
一开始没有想到状态。
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define INF 1000000
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 21
int ma[MAXN][MAXN];
int n, m, k;
int p[MAXN][MAXN][MAXN][MAXN];

int Judge(int u, int d, int l, int r)
{
    int t = 0;
    repu(i, u + 1, d + 1)
      repu(j, l + 1, r + 1)
       if(ma[i][j]) t++;
    return t;
}

int dp(int u, int d, int l, int r)
{
    if(p[u][d][l][r] != -1) return p[u][d][l][r];
    int t = Judge(u, d, l, r);
    if(t == 1) return p[u][d][l][r] = 0;
    if(t == 0) return p[u][d][l][r] = INF;
    int minn = INF;
    repu(i, u + 1, d) minn = min(minn, dp(u, i, l, r) + dp(i, d, l, r) + (r - l));
    repu(i, l + 1, r) minn = min(minn, dp(u, d, i, r) + dp(u, d, l, i) + (d - u));
    return p[u][d][l][r] = minn;
}

int main()
{
    int kase = 0;
    while(~scanf("%d%d%d", &n, &m, &k))
    {
        _cle(ma, 0);
        _cle(p, -1);
        int x, y;
        repu(i, 0, k) {
          scanf("%d%d", &x, &y);
          ma[x][y] = 1;
        }
        printf("Case %d: %d
", ++kase, dp(0, n, 0, m));
    }
    return 0;
}
View Code
 
原文地址:https://www.cnblogs.com/sunus/p/4507372.html