ACM: uva 1073 - Glenbow Museum

来源:互联网 发布:网络延迟多少ms算正常 编辑:程序博客网 时间:2024/05/16 14:29

Glenbow Museum

The famous Glenbow Museum in Calgary is WesternCanada's largest museum, with exhibits ranging from art to culturalhistory to mineralogy. A brand new section is being planned,devoted to brilliant computer programmers just like you.Unfortunately, due to lack of space, the museum is going to have tobuild a brand new building and relocate into it.

The size and capacity of the new building differfrom those of the original building. But the floor plans of bothbuildings are orthogonal polygons. An orthogonal polygon is apolygon whose internal angles are either 90° or 270°. If 90° anglesare denoted as R (Right) and 270° angles are denoted as O (Obtuse)then a string containing only R and O can roughly describe anorthogonal polygon. For example, a rectangle (Figure 1) is thesimplest orthogonal polygon and it can be described as RRRR (theangles are listed in counter-clockwise order, starting from anycorner). Similarly, a cross-shaped orthogonal polygon (Figure 2)can be described by the sequence RRORRORRORRO, RORRORRORROR, orORRORRORRORR. These sequences are called anglestrings.

ACM: <wbr>uva <wbr>1073 <wbr>- <wbr>Glenbow <wbr>Museum

 

Of course, an angle string does not completelyspecify the shape of a polygon -- it says nothing about the lengthof the sides. And some angle strings cannot possibly describe avalid orthogonal polygon (RRROR, for example).

To complicate things further, not all orthogonalpolygons are acceptable floor plans for the museum. A museumcontains many valuable objects, and these objects must be guarded.Due to cost considerations, no floor can have more than one guard.So a floor plan is acceptable only if there is a place within thefloor from which one guard can see the entire floor. Similarly, anangle string is acceptable only if it describes at least oneacceptable polygon. Note that the cross-shaped polygon in Figure 2can be guarded by someone standing in the center, so it isacceptable. Thus the angle string RRORRORRORRO is acceptable, eventhough it also describes other polygons that cannot be properlyguarded by a single guard.

Help the designers of the new building determinehow many acceptable angle strings there are of a given length.

Input 

The input file contains several test cases. Eachtest case consists of a line containing a positiveinteger L (1 L1000) , which is the desired length of an anglestring. The input will end with a line containing a singlezero.

Output 

For each test case, print a line containing thetest case number (beginning with 1) followed by the number ofacceptable angle strings of the given length. Follow the format ofthe sample output.

SampleInput 

4

6

0

SampleOutput 

Case 1: 1

Case 2: 6

 

题意: Glenbow博物馆要建直角多边形的地面, 要求这个多边形有一个点可以看到边界上每一个点. 并且

      地面每个90度的内角就标记为R,每个270度的内角标记为O, 现在给定一个L长度的RO序列, 问你

     有多少种可能满足地面序列.

 

解题思路:

     1. 你画一画就会发现, 每个270度的内角对应一个90度内角, 而且90度内角会在多出4个. 假设

        多边形的内角个数(序列长度)为n, 90度(n+4)/2个;, 270度(n-4)/2个, 并且n<3且n为奇数

        不可能满足条件.

      2.假设现在一个多边形内部存在一个点, 可以看到整个多边形的点, 那么它的上下左右的四条边

        一定是RR, 并且相邻的两条RR边之间一定存在OROROR...这样的序列(边);

      3.序列内一定不会出现OO这样的边, 问题就转化为: (n+4)/2个R, (n-4)/2个O的环状序列, 并且

        其中没有OO相邻, 有多少种序列.

        设状态: dp[i][j][k][p]表示i个R, j个O, 第一个元素是R(k=0)或O(k=1),最后一个元素R(p=0)

        或O(p=1)的序列种数.

        边界条件: dp[i][0][0][0] = dp[0][1][1][1] = 1;

        状态方程: dp[i][j][k][0] = dp[i-1][j][k][0]+dp[i-1][j][k][1];

                   dp[i][j][k][1]= dp[i][j-1][k][0];

        结果: dp[(n+4)/2][(n-4)/2][0][0]+
               dp[(n+4)/2][(n-4)/2][0][1]+
              dp[(n+4)/2][(n-4)/2][1][0])

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 1005
typedef long long ll;

int n;
ll dp[MAX][MAX][2][2];

void init()
{
 memset(dp, 0, sizeof(dp));
 dp[0][1][1][1] = 1;
 for(int x = 1; x <= 1000;++x)
  dp[x][0][0][0] = 1;

 for(int i = 1; i <= 1000;++i)
 {
  for(int j = 1; j<= 1000; ++j)
  {
   dp[i][j][0][0]= dp[i-1][j][0][0]+dp[i-1][j][0][1];
   dp[i][j][1][0]= dp[i-1][j][1][0]+dp[i-1][j][1][1];
   dp[i][j][0][1]= dp[i][j-1][0][0];
   dp[i][j][1][1]= dp[i][j-1][1][0];
  }
 }
}

int main()
{
// freopen("input.txt", "r", stdin);
 init();
 int num = 1;
 while(scanf("%d", &n) !=EOF)
 {
  if(n == 0) break;

  if(n < 4 ||n%2 == 1) printf("Case %d: 0\n", num++);
  else
  {
   printf("Case%d: %lld\n", num++,
    dp[(n+4)/2][(n-4)/2][0][0]+
    dp[(n+4)/2][(n-4)/2][0][1]+
    dp[(n+4)/2][(n-4)/2][1][0]);
  }
 }

 return 0;
}

0 0
原创粉丝点击