Ultraviolet A 10593-Kites (DP)

Source: Internet
Author: User

Link to the question: Ultraviolet A 10593-Kites

A map of N * n is given, indicating a piece of cart and asking how many methods can be used to make a kite. The kite must be a square or a diamond and cannot have holes.

Solution: the solution is divided into square and diamond:

  • Square, DP [I] [J] indicates a square in the lower right corner with I and j
    DP [I] [J] = min (DP [I? 1] [J], DP [I] [J? 1])
    And if the yellow part is 'x', DP [I] [J] ++

  • Diamond, DP [I] [J] indicates the positive direction of the Diamond

    If the yellow part of the city is 'x', DP [I] [J] ++


#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 505;int n, dp[N][N];char g[N][N];int solve () {    int ans = 0;    memset(dp, 0, sizeof(dp));    for (int i = 1; i <= n; i++) {        for (int j = 1; j <= n; j++) {            if (g[i][j] == ‘x‘) {                int tmp = min(dp[i-1][j], dp[i][j-1]);                dp[i][j] = tmp + (g[i-tmp][j-tmp] == ‘x‘);                if (dp[i][j] > 1)                    ans += dp[i][j] - 1;            }        }    }    memset(dp, 0, sizeof(dp));    for (int i = 1; i <= n; i++) {        for (int j = 1; j <= n; j++) {            if (g[i][j] == ‘x‘) {                int tmp = min(dp[i-1][j-1], dp[i-1][j+1]);                if (tmp == 0 || g[i-1][j] != ‘x‘)                    dp[i][j] = 1;                else if (g[i-tmp*2][j] == ‘x‘ && g[i-tmp*2+1][j] == ‘x‘)                    dp[i][j] = tmp + 1;                else                    dp[i][j] = tmp;                if (dp[i][j] > 1)                    ans += dp[i][j] - 1;            }        }    }    return ans;}int main () {    while (scanf("%d%*c", &n) == 1 && n) {        for (int i = 1; i <= n; i++)            gets(g[i]+1);        printf("%d\n", solve());    }    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.