Uva585-triangles (brute force enumeration)

Source: Internet
Author: User

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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.