[ACM_搜索] Triangles(POJ1471,简单搜索,注意细节)

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. 
[ACM_搜索] Triangles(POJ1471,简单搜索,注意细节)

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.

 题目意思是:给你一个高为n的三角形,其中#表示黑色,-表示白色,问这个里面最大的3角形的面积(其中每个小的3角形面积为1)。算法其实很简单,就是遍历每一个白色小三角形,如果它是朝上的(行列相加为奇数)就把它看为最大三角形的顶部,向下逐层搜索,看最大能为几层;同理当其朝下时,就逐层向上搜索,求最大层。然后用maxNum维护一个最终的最大层(注意这个数最好初始化为0!!!)。将输入3角形转换为从(1,1)开始的0-1矩阵(这样可以省去边界处理),要注意数组要开的大一点!!!

 1 #include<iostream>

 2 #include<string>

 3 #include<string.h>

 4 #include<cstring>

 5 using namespace std;

 6 int n,tab[306][306];

 7 int proNum,maxNum;

 8 bool lu(int I,int J){//向上找

 9     for(int i=0;i<2*proNum+1;i++)

10         if(!tab[I-proNum][J-proNum+i])return 0;

11     return 1;

12 }

13 bool ld(int I,int J){//向下找

14     for(int i=0;i<2*proNum+1;i++)

15         if(!tab[I+proNum][J-proNum+i])return 0;

16     return 1;

17 }

18 void dfs(int I,int J){

19     if((I+J)%2==0){//朝下,向上找

20         while(lu(I,J)){

21             proNum++;

22         }

23     }else{//朝上,向下找

24         while(ld(I,J)){

25             proNum++;

26         }

27     }

28 }

29 void solve(){

30     maxNum=0;//初始为0(针对全黑情况)

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

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

33             if(tab[i][j]){//尝试将所有白三角形当成顶点看其最大层数

34                 proNum=1;

35                 dfs(i,j);

36                 if(proNum>maxNum)maxNum=proNum;//更新

37             }

38         }

39     }

40 }

41 int main(){

42     string str;int casee=1;

43     while(cin>>n && n){

44         memset(tab,0,sizeof(tab));

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

46             cin>>str;

47             for(int j=0;j<str.size();j++){

48                 if(str[j]=='-')tab[i][j+i]=1;

49             }

50         }

51         solve();

52         cout<<"Triangle #"<<casee++<<'\n';

53         cout<<"The largest triangle area is "<<maxNum*maxNum<<".\n\n";

54     }return 0;

55 

56 }

 

 

 

 

 

 

 

 

你可能感兴趣的:(ACM)