zoukankan      html  css  js  c++  java
  • USACO3.3.4Home on the Range

    Home on the Range

    Farmer John grazes his cows on a large, square field N (2 <= N <= 250) miles on a side (because, for some reason, his cows will only graze on precisely square land segments). Regrettably, the cows have ravaged some of the land (always in 1 mile square increments). FJ needs to map the remaining squares (at least 2x2 on a side) on which his cows can graze (in these larger squares, no 1x1 mile segments are ravaged).

    Your task is to count up all the various square grazing areas within the supplied dataset and report the number of square grazing areas (of sizes >= 2x2) remaining. Of course, grazing areas may overlap for purposes of this report.

    PROGRAM NAME: range

    INPUT FORMAT

    Line 1: N, the number of miles on each side of the field.
    Line 2..N+1: N characters with no spaces. 0 represents "ravaged for that block; 1 represents "ready to eat".

    SAMPLE INPUT (file range.in)

    6
    101111
    001111
    111111
    001111
    101101
    111001
    

    OUTPUT FORMAT

    Potentially several lines with the size of the square and the number of such squares that exist. Order them in ascending order from smallest to largest size.

    SAMPLE OUTPUT (file range.out)

    2 10
    3 4
    4 1  
    题解:动态规划问题。状态转移方程为f[i][j]=min(f[i-1][j-1],f[i-1][j],f[i][j-1])+1,f[i][j]表示从右下角格子s[i][j]开始能拓展形成的最大正方形。f[i][j]=ans,表示从格子s[i][j]能找到1~ans边长的正方形。最后进行简单的统计即可。
    View Code
     1 /*
     2 ID:spcjv51
     3 PROG:range
     4 LANG:C
     5 */
     6 #include<stdio.h>
     7 #include<string.h>
     8 #define MAXN 255
     9 long f[MAXN][MAXN];
    10 char s[MAXN][MAXN];
    11 long ans[MAXN];
    12 long m,n;
    13 long min(long a,long b)
    14 {
    15     return a>b?b:a;
    16 }
    17 int main(void)
    18 {
    19     freopen("range.in","r",stdin);
    20     freopen("range.out","w",stdout);
    21     long i,j,k;
    22     scanf("%ld",&n);
    23     memset(f,0,sizeof(f));
    24     memset(ans,0,sizeof(ans));
    25     for(i=1; i<=n; i++)
    26     {
    27         getchar();
    28         for(j=1; j<=n; j++)
    29             scanf("%c",&s[i][j]);
    30     }
    31 
    32     for(i=1; i<=n; i++)
    33         for(j=1; j<=n; j++)
    34             if(s[i][j]=='1')
    35             {
    36                 f[i][j]=min(f[i-1][j-1],f[i-1][j]);
    37                 f[i][j]=min(f[i][j],f[i][j-1])+1;
    38             }
    39     for(i=1; i<=n; i++)
    40         for(j=1; j<=n; j++)
    41         {
    42             for(k=2; k<=f[i][j]; k++)
    43                 ans[k]++;
    44         }
    45     i=2;
    46     while(ans[i])
    47         printf("%ld %ld\n",i++,ans[i]);
    48     return 0;
    49 }
     
  • 相关阅读:
    Java中单态设计模式
    Google地图,Baidu地图数据供应商
    栅格数据和矢量数据
    Java学习之Iterator(迭代器)的一般用法 (转)
    highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度
    relative、absolute和float
    【转】20个令人敬畏的jQuery插件
    启动tomcat显示成功,但是访问tomcat首页是404错误页
    手机测试Android程序
    Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2951087.html
Copyright © 2011-2022 走看看