zoukankan      html  css  js  c++  java
  • UVA 10593 Kites DP

    The season of flying kites is well ahead. So what? Let us make an inventory for kites. We are given
    a square shaped sheet of paper. But many parts of this are already porous. Your challenge here is to
    count the total number of ways to cut a kite of any size from this sheet. By the way, the kite itself
    can’t be porous :-) AND . . . it must be either square shaped or diamond shaped.
    x
    x xxx xxx xxx
    xxx xxxxx xxx x.x x
    x xxx xxx xxx
    x
    In the above figure first three are valid kites but not next two.
    Input
    Input contains an integer n (n ≤ 500), which is the size of the sheet. Then follows n lines each of which
    has n characters (‘x’ or ‘.’). Here the dotted parts resemble the porous parts of the sheet. Input is
    terminated by end of file.
    Output
    Output is very simple. Only print an integer according to the problem statement for each test case in
    a new line.
    Sample Input
    4
    .xx.
    xxxx
    .xx.
    .x..
    3
    xxx
    xxx
    xxx
    Sample Output
    4
    6

    题意:给你一个 n*n的图,让你在图中找出x组成的边长大于等于2的菱形或正方

    题解:DP ,这个博客的图比较好

          http://blog.csdn.net/u012596172/article/details/41171815

    //meek
    #include<bits/stdc++.h>
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include<map>
    #include<queue>
    using namespace std ;
    typedef long long ll;
    #define mem(a) memset(a,0,sizeof(a))
    #define pb push_back
    #define fi first
    #define se second
    #define MP make_pair
    
    const int N=550;
    const ll INF = 1ll<<61;
    const int inf = 1000000007;
    const int MOD =   2000000011;
    
    char mp[N][N];
    int dp[N][N],n;
    int solve() {
        int ans = 0;
        mem(dp);
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=n;j++) {
                if(mp[i][j] == 'x') {
                    int tmp = min(dp[i-1][j],dp[i][j-1]);
                    dp[i][j] = tmp + (mp[i-tmp][j-tmp] == 'x');
                    if(dp[i][j] > 1) ans +=dp[i][j] - 1;
                }
            }
        }
        mem(dp);
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=n;j++) {
                if(mp[i][j] == 'x') {
                    int tmp = min (dp[i-1][j-1],dp[i-1][j+1]);
                    if(tmp == 0 || mp[i-1][j] != 'x')  dp[i][j] = 1;
                    else if(mp[i- 2*tmp][j]=='x' && mp[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",&n)!=EOF) {
            for(int i=1;i<=n;i++) {
                getchar();
                for(int j=1;j<=n;j++) scanf("%c",&mp[i][j]);
            }
            printf("%d
    ",solve());
        }
        return 0;
    }
    代码
  • 相关阅读:
    MongoDB查询修改操作语句命令大全
    SpringBoot读取war包jar包Resource资源文件解决办法
    linux lsof命令详解
    taskset
    POI导出excel,本地测试没问题,linux测试无法导出
    js中文乱码
    处理Account locked due to 217 failed logins的问题
    普通用户无法su到root用户
    gp数据库停止
    eclipse导入maven工程missing artifact(实际是存在的)错误解决
  • 原文地址:https://www.cnblogs.com/zxhl/p/5102933.html
Copyright © 2011-2022 走看看