欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ 1471Triangles

程序员文章站 2022-07-14 08:53:46
...

Triangles

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2112   Accepted: 776

Description

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. 

POJ 1471Triangles

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 European Regional Contest 1997

题目思维导图分析:

POJ 1471Triangles

代码如下:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;


int N,num[201][201],vis[201][201];

bool isM(int r,int c)
{
    if(c>=r&&r+c<=2*N-1&&r>=0&&c>=0)
    {
        return true;
    }
    return false;
}
void dfs(int r,int c,char **m)
{
    if((r+c)%2==1)//朝上
    {
        if(isM(r+1,c-1)&&isM(r+1,c+1)&&m[r+1][c]!='#'&&m[r+1][c-1]!='#'&&m[r+1][c+1]!='#')
        {
            vis[r][c]=true;
            if(!vis[r+1][c-1])
            {
                dfs(r+1,c-1,m);
            }
            if(!vis[r+1][c+1])
            {
                dfs(r+1,c+1,m);
            }
            num[r][c]=min(num[r+1][c-1]+1,num[r+1][c+1]+1);
        }
		else
			num[r][c]=1;
    }
    else//朝下
    {
        if(isM(r-1,c-1)&&isM(r-1,c+1)&&m[r-1][c]!='#'&&m[r-1][c-1]!='#'&&m[r-1][c+1]!='#')
        {
            vis[r][c]=true;
            if(!vis[r-1][c-1])
            {
                dfs(r-1,c-1,m);
            }
            if(!vis[r-1][c+1])
            {
                dfs(r-1,c+1,m);
            }
            num[r][c]=min(num[r-1][c-1]+1,num[r-1][c+1]+1);
        }
		else
			num[r][c]=1;
    }
}

int main()
{
	int caseCount=0;
	while(scanf("%d",&N)&&N)
	{
	    char **m=new char*[N];
	    for(int i=0;i<N;i++)
        {
            m[i]=new char[2*N];
			fill(m[i],m[i]+2*N,' ');
        }
        //memset(m,' ',sizeof(m));
		for(int i=0;i<N;i++)
			scanf("%s",(m[i]+i));//注意此处的输入
        memset(num,0,sizeof(num));
        memset(vis,false,sizeof(vis));
        int ans=0;
        for(int i=0;i<N;i++)
        {
            for(int j=i;j<2*N-1-i;j++)
            {
                if(m[i][j]=='-')
                {
                    dfs(i,j,m);
                    ans=max(ans,num[i][j]);
                }
            }
        }
		printf("Triangle #%d\n",++caseCount);
		printf("The largest triangle area is %d.\n\n",ans*ans);
		for(int i=0;i<N;i++)
        {
            delete[] m[i];
        }
        delete []m;
	}
	return 0;
}

 

上一篇: Python中selenium的使用

下一篇: list