您的位置:首页 > 其它

[USACO3.3.4]Home on the Range

2017-08-01 19:10 246 查看
Home on the RangeFarmer 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 haveravaged 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 overlapfor 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
题意:求边长为2~n的正方形在输入的里面有多少个(只包含1)
做法:枚举每个端点,作为右下角,先求出最大的边长,然后倒着推回去。
至于求当前最大的,显然,如果当前为0,就是0,如果当前为1
就是min(f[l-1,R],f[L-1,R-1],f[L,R-1])+1
这个主要是要满足整个大的是正方形。
/*6
101111
001111
111111
001111
101101
1110012 103 44 1*//*ID:cqz15311LANG:C++PROG:range*/#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;int ans[255],a[255][255],f[255][255];int n;int read(){int a = 0;char c = getchar();while (!((c>='0') && (c<='9'))) c = getchar();while ((c>='0') && (c<='9')){a = a * 10 + c - 48;c = getchar();}return a;}int main(){freopen("range.in","r",stdin);freopen("range.out","w",stdout);n = read();char c;for (int i=1;i<=n;i++){for (int j=1;j<=n;j++){c = getchar();while (!((c>='0') && (c<='1'))) c = getchar();a[i][j] = c - 48;}}memset(ans,0,sizeof(ans));memset(f,0,sizeof(f));for (int i=1;i<=n;i++){for (int j=1;j<=n;j++){if (a[i][j] == 1){f[i][j] = min(f[i-1][j],min(f[i][j-1],f[i-1][j-1]))+1;ans[f[i][j]]++;}//			printf("%d ",f[i][j]);}//		puts("");}for (int i=n-1;i>=1;i--){ans[i] += ans[i+1];}for (int i=2;i<=n;i++){if (ans[i] > 0){printf("%d %d\n",i,ans[i]);}}fclose(stdin);fclose(stdout);return 0;} 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  动态规划