875. The range of home

description

John farmers a side length N (2 <= N <= 250) grazing his cows on pasture square miles. (For some reason, only his cows graze on the pasture square.) Unfortunately, his cows had destroyed some land. (Some of the 1-square-mile square)

Farmer John may need to count those squares pasture grazing cows (at least 2x2, and is not a point to be destroyed in such a large square, that is, all the points are "1").

Your job to count all the different squares grazing area number (> = 2x2) in the data set is supplied inside. Of course, grazing areas may overlap.

format

PROGRAM NAME: range

INPUT FORMAT:

(file range.in)

Line 1: the length N, pastoral.

2 to row n + 1: N number of characters not separated by spaces.

0 means "a section that was destroyed"; 1 means "ready to be eaten."

OUTPUT FORMAT:

(file range.out)

Those present in the output of the square side length and the number of a row.

SAMPLE INPUT

6
101111
001111
111111
001111
101101
111001

SAMPLE OUTPUT

10 2 
. 3. 4 
. 4. 1 ******************************************** ****************************************** Analysis: This question, a move Regulation problems, the state transition equation: f [i] [j] = min (f [i + 1] [j], f [i] [j + 1], f [i + 1] [j + 1]) + 1; (wherein i and j represents the position of the element, F [i] [j] denotes the largest element in the upper left corner of the square side length) of this state transition equation is obtained by the following recursive upwardly, i.e., only when the four square directions meet is to 1, the next step is to expand successively upwards recursion to obtain the number of different squares, TOT using a one-dimensional array [] record. ************************************************** ************************************






. 1 #include <the iostream>
 2 #include <cstdio>
 . 3 #include < String >
 . 4 #include <CString>
 . 5  the using  namespace STD;
 . 6  
. 7  int n-, I, K, J, F [ 500 ] [ 500 ], TOT [ 500 ] = { 0 }; // variables described constants 
. 8  char STR [ 500 ] [ 500 ]; // variables described constants 
. 9  int min3 ( int A, int B, int C)
 10  {
 . 11     int f=a;
12     if(f>b)f=b;
13     if(f>c)f=c;
14     return f;
15 }
16 
17 
18 int main()
19 {
20      freopen("range.in","r",stdin);
21     freopen("range.out","w",stdout);
22     cin>>n;
23 
24 
25     getchar();
26     for(i=0;i<n;i++)
27     {
28         for(j=0;j<n;j++)
29          cin>>str[i][j];
30         getchar();
31     }
32 
33     for(i=0;i<n;i++)
34      for(j=0;j<n;j++)
35           f[i][j]=str[i][j]-'0';//初始化
36      for(i=n-1;i>=0;i--)
37        for (J = N- . 1 ; J> = 0 ; J, )
 38 is            IF (STR [I] [J] == ' . 1 ' )
 39              {
 40                  // recursive determined this point as the upper left vertex of a square the maximum side length of 
41 is  
42 is                  F [I] [J] = min3 (F [I + . 1 ] [J], F [I] [J + . 1 ], F [I + . 1 ] [J + . 1 ]) + . 1 ;
 43 is                  IF ( F [I] [J]> . 1 )
 44 is                  for (K = 2 ; K <= F [I] [J]; K ++ )
 45                    TOT [K] ++; // number of different storage side length of the square
46             }
47     for(i=2;i<=n;i++)
48      if(tot[i]!=0)
49       cout<<i<<' '<<tot[i]<<endl;
50     return 0;
51 }
View Code

 

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3150790.html

Guess you like

Origin blog.csdn.net/weixin_34293911/article/details/93432956