/*
动态三角形:每次DP时考虑的是两个子三角形的高度即可
注意: 三角形可以是倒置的。
*/
View Code
1 #include <iostream>
2 #include <cstdlib>
3 #include <cstring>
4 #include <stdio.h>
5
6 using namespace std;
7
8 char Tri[ 101 ][ 201 ];
9 short Val[ 101 ][ 201 ];
10
11 int main()
12 {
13 int n,cases = 1;
14 while ( scanf("%d",&n) && n ) {
15 getchar();
16 for ( int i = 0 ; i < n ; ++ i ) {
17 int m = 0;
18 while ( ( Tri[ i ][ m ] = getchar() ) != '\n' ) ++ m;
19 }
20
21 memset( Val, 0, sizeof( Val ) );
22 for ( int i = 0 ; i < 2*n ; ++ i )
23 if ( Tri[ 0 ][ i ] == '#' )
24 Val[ 0 ][ i ] = 0;
25 else
26 Val[ 0 ][ i ] = 1;
27 for ( int i = 1 ; i < n ; ++ i )
28 for ( int j = i ; j < 2*n-1-i ; j += 2 )
29 if ( Tri[ i ][ j ] == '#' )
30 Val[ i ][ j ] = 0;
31 else if ( Tri[ i-1 ][ j ] == '#' )
32 Val[ i ][ j ] = 1;
33 else
34 Val[ i ][ j ] = 1+min( Val[ i-1 ][ j-1 ], Val[ i-1 ][ j+1 ] );
35
36 if ( Tri[ n-2 ][ 2*n-3 ] == '#' )
37 Val[ n-2 ][ 2*n-3 ] = 0;
38 else
39 Val[ n-2 ][ 2*n-3 ] = 1;
40
41 for ( int i = n-3 ; i >= 0 ; -- i )
42 for ( int j = i+1 ; j < 2*n-2-i ; j += 2 )
43 if ( Tri[ i ][ j ] == '#' )
44 Val[ i ][ j ] = 0;
45 else if ( Tri[ i+1 ][ j ] == '#' )
46 Val[ i ][ j ] = 1;
47 else
48 Val[ i ][ j ] = 1+min( Val[ i+1 ][ j-1 ], Val[ i+1 ][ j+1 ] );
49
50 /*
51 for ( int i = 0 ; i < n ; ++ i ) {
52 for ( int j = 0 ; j < 2*n-i-1 ; ++ j )
53 cout << Val[ i ][ j ] << " ";
54 cout << endl;
55 }
56 */
57
58 int Max = 0;
59 for ( int i = 0 ; i < n ; ++ i )
60 for ( int j = i ; j < 2*n-i-1 ; ++ j )
61 if ( Val[ i ][ j ] > Max )
62 Max = Val[ i ][ j ];
63
64 cout << "Triangle #" << cases ++ << endl;
65 cout << "The largest triangle area is ";
66 cout << Max*Max << "." << endl << endl;
67 }
68 return 0;
69 }