zoukankan      html  css  js  c++  java
  • ZOJ 1245 Triangles


    Triangles

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    It is always very nice to have little brothers or sisters. You can tease them, lock them in the bathroom or put red hot chili in their sandwiches. But there is also a time when all meanness comes back!

    As you know, in one month it is Christmas and this year you are honored to make the big star that will be stuck on the top of the Christmas tree. But when you get the triangle-patterned silver paper you realize that there are many holes in it. Your little sister has already cut out smaller triangles for the normal Christmas stars. Your only chance is to find an algorithm that tells you for each piece of silver paper the size of the largest remaining triangle.

    Given a triangle structure with white and black fields inside you must find the largest triangle area of white fields, as shown in the following figure.

    ZOJ 1245 Triangles - qhn999 - 码代码的猿猿

    Input

    The input contains several triangle descriptions. The first line of each description contains an integer n (1 <= n <= 100), which gives the height of the triangle. The next n lines contain characters of the set {space, #, -} representing the rows of the triangle, where `#' is a black and `-' a white field. The spaces are used only to keep the triangle shape in the input by padding at the left end of the lines. (Compare with the sample input. The first test case corresponds to the figure.) 
    For each triangle, the number of the characters `#' and `-' per line is odd and decreases from 2n - 1 down to 1.

    The input is terminated by a description starting with n = 0.


    Output 

    For each triangle in the input, first output the number of the triangle, as shown in the sample output. Then print the line ``The largest triangle area is a.'', where a is the number of fields inside the largest triangle that consists only of white fields. Note that the largest triangle can have its point at the top, as in the second case of the sample input.

    Output a blank line after each test case.


    Sample Input

    5
    #-##----#
     -----#-
      ---#-
       -#-
        -
    4
    #-#-#--
     #---#
      ##-
       -
    0


    Sample Output

    Triangle #1
    The largest triangle area is 9.

    Triangle #2
    The largest triangle area is 4.


    Source: Southwestern Europe 1997

    要找出这里边的由白色三角形组成的最大面积,即找出白色的连续的最大层数;
    三角形分两种:一种是尖朝上,另一种是尖朝上。
    数组dp[j]里边存的是以这个三角形为顶点时所能达到的三角形的最大层数;
    (1)当map[j]为白色时:
         当map[i+1][j]为白色时dp[j]=min(dp[i+1][j-1],dp[i+1][j+1])+1;
         当map[i+1][j]为黑色时dp[j]=1;

    (2)当map[j]为黑色时:
         dp[j]=0;
    上边是分析当三角形尖朝上的情况,尖朝下的类似,只是要看第i-1行的,同时要注意要将整个数组赋初值为0;
    还有开数组时n<=100所以数组最小为dp[n][2*n-1];




    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;
    const int MAXN=210;
    char mp[MAXN][2*MAXN];
    int dp[MAXN][2*MAXN];

    int n;

    int main()
    {
        int cnt=0;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;
        memset(mp,' ',sizeof(mp));
        for(int i=1;i<=n;i++)
        {
            int m=(2*n-1)-(i-1)*2+i-1;
            for(int j=0;j<=m;j++)
            {
                scanf("%c",&mp[j]);
            }
        }

        memset(dp,0,sizeof(dp));

        int ans=0;

        ///尖头朝上的三角形的最大层数
        for(int i=n;i>=1;i--)
        {
            int m=2*n;
            for(int j=i+1;j<=m-i;j+=2)
            {
                if(mp[j]=='-')
                {
                    if(mp[i+1][j]=='-')
                    {
                        dp[j]=min(dp[i+1][j-1],dp[i+1][j+1])+1;
                    }
                    else if(mp[i+1][j]!='-')
                    {
                        dp[j]=1;
                    }
                    ans=max(ans,dp[j]);
                }
            }
        }

        memset(dp,0,sizeof(dp));

        ///尖头朝下的三角形的最大层数
        for(int i=1;i<=n;i++)
        {
            int m=2*n;
            for(int j=i;j<=m-i;j+=2)
            {
                if(mp[j]=='-')
                {
                    if(mp[i-1][j]=='-')
                    {
                        dp[j]=min(dp[i-1][j-1],dp[i-1][j+1])+1;
                    }
                    else if(mp[i-1][j]!='-')
                    {
                        dp[j]=1;
                    }
                    ans=max(ans,dp[j]);
                }
            }
        }

        printf("Triangle #%d ",++cnt);
        printf("The largest triangle area is %d. ",ans*ans);

    }

        return 0;
    }


  • 相关阅读:
    背景图像固定(背景附着)
    css背景图片位置
    2.使用第三种方式做一个多线程操作 3. 使用线程池做一个1到100的偶数之和 4.写一遍生产者与消费者模式 5 写一个字符串的单例设置模式(未完成) 6. 写一个简单工厂着模式
    简单写写
    说说JSON和JSONP区别
    web前端常见的面试题,基础知识点
    优秀网页设计_优秀Web设计的69条设计原则
    PostCSS_自动处理css3属性前缀
    用lnmp架构部署wordpress网站详细步骤
    使用html+css+js实现简易计算器
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350982.html
Copyright © 2011-2022 走看看