USACO / Home on the Range (DP统计正方形个数)
2012-08-02 17:52
441 查看
描述
农民约翰在一片边长是N(2<=N<=250)英里的正方形牧场上放牧他的奶牛。(因为一些原因,他的奶牛只在正方形的牧场上吃草。)遗憾的是,他的奶牛已经毁坏一些土地。(一些1平方英里的正方形)农民约翰需要统计那些可以放牧奶牛的正方形牧场(至少是2x2的,在这些较大的正方形中没有一个点是被破坏的,也就是说,所有的点都是“1”)。你的工作要在被供应的数据组里面统计所有不同的正方形放牧区域(>=2x2)的个数。当然,放牧区域可能是重叠。格式
PROGRAMNAME:rangeINPUTFORMAT:(filerange.in)第1行:N,牧区的边长。第2到 n+1行:N个没有空格分开的字符。0表示"那一个区段被毁坏了";1表示"准备好被吃"。OUTPUTFORMAT:(filerange.out)输出那些存在的正方形的边长和个数,一种一行。SAMPLEINPUT
6 101111 001111 111111 001111 101101 111001
SAMPLEOUTPUT
210 34 41 分析: 统计各边长正方形个数,简单DP,设f[i][j]表示左上角顶点为(i,j)的正方形最大边长。则f[i][j]=min{f[i][j+1],f[i+1][j],f[i+1][j+1]}+1。
USER:ZhipengZHANG[138_3531] TASK:range LANG:C++ Compiling... Compile:OK Executing... Test1:TESTOK[0.000secs,3532KB] Test2:TESTOK[0.000secs,3528KB] Test3:TESTOK[0.000secs,3528KB] Test4:TESTOK[0.000secs,3528KB] Test5:TESTOK[0.000secs,3528KB] Test6:TESTOK[0.011secs,3528KB] Test7:TESTOK[0.022secs,3532KB] AlltestsOK.
Yourprogram('range')producedallcorrectanswers!Thisisyoursubmission#2forthisproblem.Congratulations!
/*
ID:138_3531
LANG:C++
TASK:range
*/#include<fstream>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<fstream>
#include<queue>
#include<climits>
#include<vector>
#include<map>
#include<cmath>usingnamespacestd;intMin(inta,intb)
{
returna>b?b:a;
}intmain()
{
freopen("range.in","r",stdin);
freopen("range.out","w",stdout);intn;
chars[300][300];
intf[300][300];
intnum[300];//每个边长正方形数目
memset(num,0,sizeof(num));
cin>>n;
memset(f,0,sizeof(f));
for(inti=0;i<n;i++)
for(intj=0;j<n;j++)
{
cin>>s[i][j];
}
for(inti=n-1;i>=0;i--)
for(intj=n-1;j>=0;j--)
{
if(s[i][j]-'0')
{
f[i][j]=f[i+1][j];
f[i][j]=Min(f[i][j],f[i][j+1]);
f[i][j]=Min(f[i][j],f[i+1][j+1]);
f[i][j]++;
}//统计
for(intk=2;k<=f[i][j];k++)
num[k]++;}
for(inti=2;i<=n;i++)
if(num[i])
cout<<i<<""<<num[i]<<endl;
}
相关文章推荐
- Home on the Range USACO 3.3 (dp阵亡)
- USACO-Section 3.3 Home on the Range (树状数组||DP)
- USACO Section 3.3 Home on the Range(dp)
- USACO3.34Home on the Range(DP)
- USACO--3.3Home on the Range+DP
- usaco Home on the Range java ---DP
- Home On The Range_usaco3.3_dp
- USACO3.3.4 Home on the Range (range)
- USACO Section 3.3 Home on the Range
- USACO3.3.4 Home on the Range (range)
- USACO Home on the Range
- usaco Home on the Range
- USACO-Section3.3 Home on the Range【完全背包】
- USACO-Section 3.3- PROB Home on the Range
- USACO home on the range C 语言实现
- USACO 3.3 Home on the Range(最大子矩阵问题)
- USACO 3.3 Home on the Range (range)
- USACO Section 3.3 Home on the Range - 优化的BFS..
- usaco Home on the Range
- USACO3.3.4--Home on the Range