求白色三角形的最大面积 Triangles

Triangles

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

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

Given a triangle structure with white andblack fields inside you must find the largest triangle area of white fields, asshown in the following figure.


Input

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

The input is terminated by a descriptionstarting with n = 0.


Output 

For each triangle in the input, first output the number of the triangle, asshown in the sample output. Then print the line ``The largest triangle area isa.'', where a is the number of fields inside the largest triangle that consistsonly of white fields. Note that the largest triangle can have its point at thetop, 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.

 

题目意思描述:找出图中白色三角形的最大面积,并输出。题目中用#表示黑色的三角形,用-表示白色三角形。

解题思路:最初想按照三角形的三个表来扩展查找,发现太麻烦了。然后仔细观察图中的三角形可知,图中的三角形只有两种形式:向上的三角形和向下的三角形(以顶点区分)。如果用node[i][i]开始存储第i行输入的数据,我们可以发现:

1、对任意一结点node[i][j](i,j都合理),如果i+j为偶数,那么该三角形是向下的,反之为向上的。

2、以某个三角形为开头的三角形面积为其对应最大三角形层数的平方,所以,用数组存储该点为三角形开头的最大三角形层数。

所以我们从向上和向下两个方向寻找最大三角形层数。以向下为例:

a[i][j]如果是白色,就判断正上方(正下方)是否为白色,如果是,就在正上方(正下方)三角形两侧得三角形中取最小层数,加在a[i][j]上;最后求出最大层数,计算出面积。

具体代码如下:

 

#include<iostream>

usingnamespace std;

 

#definemax 101

 

typedefstruct

{

    int level;//当前结点的最大层数

    char ch;//当前结点的符号表示

}Node;

Nodenode[2*max][2*max];

 

 

intmain()

{

    int n;//n表示输入的值

    int cishu=1;

    cin>>n;

    while(n!=0)

    {

        for(int i=0;i<2*n;i++)

        {

            for(int j=0;j<2*n;j++)

            {

                node[i][j].level=0;

                node[i][j].ch=' ';

            }

        }

        for(int i=0;i<n;i++)

        {

            for(int j=i;j<2*n-i-1;j+=1)

            {

                cin>>node[i][j].ch;

                if(node[i][j].ch=='-')

                    node[i][j].level=1;

            }

        }

        //往下寻找三角形

        for(int i=1;i<n;i++)

        {

            for(int j=i;j<2*n-i-1;j+=2)

            {

                //i+j为jishu

                if(node[i][j].ch =='-')

                {

                    if(node[i-1][j].ch=='-')

                    {

                        node[i][j].level+=node[i-1][j-1].level<node[i-1][j+1].level?node[i-1][j-1].level:node[i-1][j+1].level;

                    }

                }

            }

        }

        //往上寻找三角形

        for(int i=n-3;i>=0;i--)

        {

            for(int j=i+1;j<2*n-i-1;j+=2)

            {

                if(node[i][j].ch =='-')

                {

                    if(node[i+1][j].ch=='-')

                    {

                        node[i][j].level+=node[i+1][j-1].level<node[i+1][j+1].level?node[i+1][j-1].level:node[i+1][j+1].level;

                    }

                }

            }

        }

        int maxn=0;

        for(int i=0;i<n;i++)

        {

            for(int j=i;j<2*n-i-1;j+=1)

            {

                if(node[i][j].level>maxn)

                    maxn=node[i][j].level;

            }

        }

        cout<<"Triangle#"<<cishu<<endl;

        cishu+=1;

        cout<<"The largest trianglearea is "<<maxn*maxn<<"."<<endl;

        cout<<endl;

        cin>>n;//下一次输入

    }

    return 0;

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值