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
dp[i][j]表示右下角为(i,j)的正方形最大的边长
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #define name "range" using namespace std; int a[255][255]; int dp[255][255],cnt[255]; int main() { freopen(name ".in","r",stdin); freopen(name ".out","w",stdout); int i,j,k,n;char c; cin>>n; for (i=1;i<=n;i++) for (j=1;j<=n;j++) { cin>>c; if (c=='1') a[i][j]=1; } for (i=1;i<=n;i++) for (j=1;j<=n;j++) if (a[i][j]==1) dp[i][j]=min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]))+1; for (i=1;i<=n;i++) for (j=1;j<=n;j++) for (k=2;k<=dp[i][j];k++) cnt[k]++; for (i=2;i<=n;i++) if (cnt[i]!=0) cout<<i<<" "<<cnt[i]<<endl; return 0; } /* Executing... Test 1: TEST OK [0.000 secs, 4688 KB] Test 2: TEST OK [0.000 secs, 4688 KB] Test 3: TEST OK [0.000 secs, 4688 KB] Test 4: TEST OK [0.000 secs, 4688 KB] Test 5: TEST OK [0.000 secs, 4688 KB] Test 6: TEST OK [0.022 secs, 4688 KB] Test 7: TEST OK [0.032 secs, 4688 KB] All tests OK. YOUR PROGRAM ('range') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations. */