Question Link
Find the area of the biggest triangle that is not colored in the given graph.
Idea: You can observe the figure carefully and find that the formation of a large triangle is based on an unstained small triangle, and then layer-by-layer overlays (if the entire layer is not stained ). Looking at the image from the top down, it is not difficult to find that if the bottom side of a small triangle is up, the superposition is formed up, the bottom side is down, and the superposition is formed down. Therefore, we can enumerate every small triangle that is not colored, and update the maximum number of layers that can be superimposed to obtain the maximum Triangle Area.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 1005;char str[MAXN][MAXN];int g[MAXN][MAXN];int n, ans, dir;void init() { ans = 0; getchar(); memset(g, -1, sizeof(g)); for (int i = 0; i < n; i++) { gets(str[i]); for (int j = 0; j < strlen(str[i]); j++) if (str[i][j] == '-') g[i][j] = 0; }}void dfs(int x, int y, int cnt) { if (x < 0 || x > n) { ans = max(ans, cnt); return; } for (int i = y - cnt; i <= y + cnt; i++) { if (g[x][i] == -1) { ans = max(ans, cnt); return; } } dfs(x + dir, y, cnt + 1); }int main() { int t = 0; while (scanf("%d", &n) == 1 && n) { init(); for (int i = 0; i < n; i++) { for (int j = i; j < 2 * (n - i) + i - 1; j++) { if (!g[i][j]) { if ((i + j) % 2) { dir = 1; dfs(i, j, 0); } else { dir = -1; dfs(i, j, 0); } } } } printf("Triangle #%d\n", ++t); printf("The largest triangle area is %d.\n\n", ans * ans); } return 0;}