分两个方向dp。f[i][j]记录以(i,j)为顶点能达到的三角形最大高度。转移时,取(i,j)“肩上”两个的较小值(当时如果(i,j)正上方是黑色的,那么f[i][j] = 1).
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MaxN = 100 * 2 + 5;
bool mp[MaxN][MaxN];
char s[MaxN];
int n;
int f[MaxN][MaxN];
int gao()
{
memset(f, 0, sizeof(f));
for(int i = 1; i <= n; i++) {
for(int j = i; j < 2 * n - i + 1; j += 2) {
if(mp[i][j] && mp[i - 1][j]) {
if(f[i - 1][j - 1] < f[i - 1][j + 1]) f[i][j] = f[i - 1][j - 1] + 1;
else f[i][j] = f[i - 1][j + 1] + 1;
}
else if(mp[i][j]) f[i][j] = 1;
}
}
for(int i = n - 1; i >= 1; i--) {
for(int j = i + 1; j < 2 * n - i + 1; j += 2) {
if(mp[i][j] && mp[i + 1][j]) {
if(f[i + 1][j - 1] < f[i + 1][j + 1]) f[i][j] = f[i + 1][j - 1] + 1;
else f[i][j] = f[i + 1][j + 1] + 1;
}
else if(mp[i][j]) f[i][j] = 1;
}
}
int res = 0;
for(int i = 1; i <= n; i++) for(int j = i; j < 2 * n - i + 1; j++) {
if(res < f[i][j]) res = f[i][j];
}
return res * res;
}
int main()
{
int cas = 1;
while(1 == scanf("%d", &n) && n) {
memset(mp, false, sizeof(mp));
getchar();
for(int i = 1; i <= n; i++) {
gets(s + 1);
int len = strlen(s + 1);
for(int j = 1; j <= len; j++) {
if(s[j] == '-') mp[i][j] = true;
}
}
printf("Triangle #%d\n", cas++);
printf("The largest triangle area is %d.\n", gao());
puts("");
}
return 0;
}