码迷,mamicode.com
首页 > 其他好文 > 详细

uva 1629

时间:2015-05-16 10:22:32      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

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.

 

技术分享<tex2html_verbatim_mark>

One allowable slicing is as follows.

 

=6in 技术分享<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 技术分享<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技术分享nm技术分享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\n", ++kase, dp(0, n, 0, m));
    }
    return 0;
}
View Code

 

 

uva 1629

标签:

原文地址:http://www.cnblogs.com/sunus/p/4507372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!