题意:有个倒三角形如图所示
要求最大的全部是白色小三角形组成的倒三角形的小三角形的数量。输入-表示白色,#表示黑色。
题解:暴力枚举顶点的找最大解,注意每行从0开始计小三角形,编号为偶数的只能往上面找,编号为奇数的只能往下面找。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 105;
char s[N][N + N];
int n, res;
void Count(int x, int y, int k) {
int temp = 1;
if (k % 2 == 0) {
int x1 = x, y1 = y, y2 = y;
while (1) {
x1--;
y1--;
y2++;
int i;
if (x1 < 0 || y1 < x1 || y2 >= 2 * n - x1 - 1)
break;
for (i = y1; i <= y2; i++)
if (s[x1][i] != '-')
break;
if (i != y2 + 1)
break;
temp += y2 - y1 + 1;
}
}
else {
int x1 = x, y1 = y, y2 = y;
while (1) {
x1++;
y1--;
y2++;
int i;
if (x1 >= n || y1 < 0 || y2 >= 2 * n - x1 - 1)
break;
for (i = y1; i <= y2; i++)
if (s[x1][i] != '-')
break;
if (i != y2 + 1)
break;
temp += y2 - y1 + 1;
}
}
res = max(res, temp);
}
int main() {
int cas = 1;
while (scanf("%d", &n) == 1 && n) {
for (int i = 0; i < n; i++)
scanf("%s", s[i] + i);
res = 0;
for (int i = 0; i < n; i++)
for (int j = i, k = 0; j < 2 * n - i - 1; j++, k++)
if (s[i][j] == '-')
Count(i, j, k);
printf("Triangle #%d\nThe largest triangle area is %d.\n\n", cas++, res);
}
return 0;
}