这题是一道比较简单的动态规划,类似的可以有最大子矩形和。
与另外一道triangles也比较像。
#include
<
iostream
>
#define N 201
using namespace std;
char gh[N][N];
int row,col,n;
int rf[N][N][ 2 ];
void input()
{
int i,j;
for (i = 0 ;i < n;i ++ )
{
for (j = 0 ;j < 2 * n - i * 2 - 1 ;j ++ )
{
cin >> gh[i][j];
}
}
for (i = 0 ;i < n;i ++ )
{
for (j = 0 ;j < col;j ++ )
{
rf[i][j][ 0 ] = rf[i][j][ 1 ] =- 1 ;
}
}
}
int f ( int i, int j, int k)
{
if (i < 0 || i >= n || j < 0 || j >= col - i * 2 ) return 0 ;
if (i == 0 ) return gh[i][j] == ' - ' ;
if (gh[i][j] == ' # ' ) return 0 ;
if (rf[i][j][k] !=- 1 ) return rf[i][j][k];
if (gh[i - 1 ][j + 1 ] == ' # ' )
{
rf[i][j][k] = 1 ;
}
else
{
rf[i][j][k] = 1 + min(f(i - 1 ,j,k),f(i - 1 ,j + 2 ,k));
}
return rf[i][j][k];
}
int g( int i, int j, int k)
{
if (i < 0 || i >= n || j < 0 || j >= col - i * 2 ) return 0 ;
if (i == n - 2 ) return gh[i][j] == ' - ' ;
if (gh[i][j] == ' # ' ) return 0 ;
if (rf[i][j][k] !=- 1 ) return rf[i][j][k];
if (gh[i + 1 ][j - 1 ] == ' # ' )
{
rf[i][j][k] = 1 ;
}
else
{
rf[i][j][k] = 1 + min(g(i + 1 ,j,k),g(i + 1 ,j - 2 ,k));
}
return rf[i][j][k];
}
int dp()
{
int mV = 0 ,t,i,j;
for (i = 0 ;i < n;i ++ )
{
for (j = 0 ;j < col - 2 * i;j += 2 )
{
if ((t = f(i,j, 0 )) > mV)
mV = t;
}
for (j = 1 ;j < col - 2 * i;j += 2 )
{
if ((t = g(i,j, 1 )) > mV)
mV = t;
}
}
return mV;
}
int main()
{
int result;
int ncase = 0 ;
while (cin >> n)
{
ncase ++ ;
if (n == 0 ) break ;
row = n;
col = 2 * n - 1 ;
input();
result = dp();
cout << " Triangle # " << ncase << endl;
cout << " The largest triangle area is " << result * result << " . " << endl << endl;
}
return 0 ;
}
#define N 201
using namespace std;
char gh[N][N];
int row,col,n;
int rf[N][N][ 2 ];
void input()
{
int i,j;
for (i = 0 ;i < n;i ++ )
{
for (j = 0 ;j < 2 * n - i * 2 - 1 ;j ++ )
{
cin >> gh[i][j];
}
}
for (i = 0 ;i < n;i ++ )
{
for (j = 0 ;j < col;j ++ )
{
rf[i][j][ 0 ] = rf[i][j][ 1 ] =- 1 ;
}
}
}
int f ( int i, int j, int k)
{
if (i < 0 || i >= n || j < 0 || j >= col - i * 2 ) return 0 ;
if (i == 0 ) return gh[i][j] == ' - ' ;
if (gh[i][j] == ' # ' ) return 0 ;
if (rf[i][j][k] !=- 1 ) return rf[i][j][k];
if (gh[i - 1 ][j + 1 ] == ' # ' )
{
rf[i][j][k] = 1 ;
}
else
{
rf[i][j][k] = 1 + min(f(i - 1 ,j,k),f(i - 1 ,j + 2 ,k));
}
return rf[i][j][k];
}
int g( int i, int j, int k)
{
if (i < 0 || i >= n || j < 0 || j >= col - i * 2 ) return 0 ;
if (i == n - 2 ) return gh[i][j] == ' - ' ;
if (gh[i][j] == ' # ' ) return 0 ;
if (rf[i][j][k] !=- 1 ) return rf[i][j][k];
if (gh[i + 1 ][j - 1 ] == ' # ' )
{
rf[i][j][k] = 1 ;
}
else
{
rf[i][j][k] = 1 + min(g(i + 1 ,j,k),g(i + 1 ,j - 2 ,k));
}
return rf[i][j][k];
}
int dp()
{
int mV = 0 ,t,i,j;
for (i = 0 ;i < n;i ++ )
{
for (j = 0 ;j < col - 2 * i;j += 2 )
{
if ((t = f(i,j, 0 )) > mV)
mV = t;
}
for (j = 1 ;j < col - 2 * i;j += 2 )
{
if ((t = g(i,j, 1 )) > mV)
mV = t;
}
}
return mV;
}
int main()
{
int result;
int ncase = 0 ;
while (cin >> n)
{
ncase ++ ;
if (n == 0 ) break ;
row = n;
col = 2 * n - 1 ;
input();
result = dp();
cout << " Triangle # " << ncase << endl;
cout << " The largest triangle area is " << result * result << " . " << endl << endl;
}
return 0 ;
}