Home on the range -- optimization and dynamic graph Planning

Source: Internet
Author: User

Home on the range

 Home Scope
Farmer John grazes his cows on a square farm with a side length of N (2 <= n <= 250) miles.
(For some reason, his cows only graze on a square farm .)
Unfortunately, his cows have destroyed some land. (Some square with 1 square miles)
John, a farmer, needs to count square pastures that can graze cows (at least 2X2, where no portion less than 1x1 in these larger squares is split and destroyed ).
Your job is to count the number of different square grazing areas (> 2x2) in the supplied data group.
Of course, grazing areas may overlap.

Program name: Range

Input Format

Row 3:

N, edge length of the pastoral area.

2nd to n + 1 rows:

N characters without spaces.

0 indicates "that the Section is damaged"; 1 indicates "Prepare to be eaten ".

Sample input (File range. In)
6
101111
001111
111111
001111
101101
111001

Output Format
Output the size and number of existing squares, one row.

Sample output (File range. out)
2 10
3 4
4 1
 

Program idea: set the number f [k] [I] [J], K as the side length of a square, and I and j as the vertex coordinates in the lower right corner of a square. A [I] [J] stores the 0-1 matrix.

1. Initialization: F [1] [I] [J] = A [I] [J].

2. Then judge f [k-1] [I-1] [J-1] can be obtained (side length is a small square of the k-1), if you can, and then add a circle around the square

It is a square with a side length of K.

3. check whether there is 0 in this circle. If yes, F [k] [I] [J] = 0; otherwise, F [k] [I] [J] = 1.

The idea is correct, but the third step has to be judged one by one in a loop. Obviously, the efficiency is not high. Moreover, this is not a typical dynamic planning algorithm.

Dynamic Planning: steps 2 and 3 have changed:

If (F [k-1] [I-1] [J] & F [k-1] [I] [J-1] & F [k-1] [I-1] [J-1] & F [k-1] [I] [J])

F [k] [I] [J] = 1;

Else f [k] [I] [J] = 0;

That is to judge the side length is K of the four corners of the square above the length of the K-1 square can be obtained. In addition, the dimension can be reduced.

 

 

Code

 1 # include <stdio. h>
2 # include <stdlib. h>
3 int f [251] [251], N;
4 int sum [251] = {0 };
5 char a [251];
6
7 int main (){
8 file * In, * out;
9 In = fopen ("input6.txt", "R ");
10 out = fopen ("output.txt", "W ");
11 int I, J, K, X, flag;
12
13 fscanf (in, "% d", & N );
14 For (I = 1; I <= N; I ++)
15 {
16 fscanf (in, "% s", );
17 For (j = 1; j <= N; j ++)
18 F [I] [J] = A [J-1]-'0 ';
19}
20
21 For (k = 2; k <= N; k ++)
22 For (I = N; I> = K; I --) ---- the loop needs to be written backwards.
23 For (j = N; j> = K; j --)
24 {
25 if (F [I] [J-1] & F [I] [J] & F [I-1] [J-1] & F [I-1] [J])
26 {
27 F [I] [J] = 1;
28 sum [k] ++;
29}
30 else f [I] [J] = 0;
31}
32
33 for (I = 2; I <= N; I ++)
34 if (sum [I])
35 fprintf (Out, "% d \ n", I, sum [I]);
36 fclose (in );
37 fclose (out );
38 return 0;
39}
40

Conclusion: We need to think carefully about how to optimize the program and make some progress in finding the status. Continue to work hard.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.