The answer to another first question in the Netease Programming Competition (Java)

Source: Internet
Author: User
Tags ranges

It is said that when you walk to work, you pass through a farmland where radishes are planted. The shape of this field is a rectangular grid. The J-character of the I-th element of the field, indicating the I-th character of the field
The number of radishes contained in the grid in column J of the row. We define a lattice as the sum of the numbers of radishes in all its surrounding grids;
The grids around it include the upper, lower, left, and diagonal adjacent grids. There are a maximum of eight grids, and there will be fewer grids on the boundary of the land. If there is no grid around a grid, it is special to 0.
Return the number of all grids (including a and B) between A and B in a field to a particular extent ).
Definition
Class: numberfield
Method: countspecialnumbers
Parameters: String [], Int, int
Returns: int
Method Signature: int countspecialnumbers (string [] field, int A, int B)
(Be sure your method is public)

Constraints
-Field contains 1 to 50 elements, including 1 and 50.
-Each field element contains 1 to 50 characters, including 1 and 50.
-Each element of field contains the same number of characters.
-The characters of each field element are from '0' to '9.
-A ranges from 0 to 100, including 0 and 100.
-B ranges from A to 100, including a and 100.
Examples
0)
{& Quot; 111 & quot ",
"111 ",
"111 "}
4
8

Returns: 5
In this field, the lattice in the corner is 3, the lattice in the middle is 8, and the four other grids are 5.

1)
{& Quot; 111 & quot ",
"141 ",
"111 "}
4
8

Returns: 9
Now all nine grids meet the requirements.

2)
{& Quot; 2309 & quot ",
"0239 ",
"2314 "}
5
7

Returns: 3

3)
{& Quot; 924 & quot ",
"231 ",
"390 ",
"910 ",
"121 "}
31
36

Returns: 0

4)
{"5 "}
3
8

Returns: 0

5)
{& Quot; 1234567890 & quot ",
"1234567890 ",
"1234567890 ",
"1234567890 ",
"1234567890 ",
"1234567890 ",
"1234567890 ",
"1234567890 ",
"1234567890 ",
"1234567890 ",
"1234567890 "}
3
18

Returns: 26

 

 

Public class numberfield {
Public static void main (string [] ARGs ){
Numberfield n = new numberfield ();
String Field [] = {"1234567890", "1234567890", "1234567890 ",
"1234567890", "1234567890", "1234567890", "1234567890 ",
"1234567890", "1234567890", "1234567890", "1234567890 "};
System. Out. println (N. countspecialnumbers (field, 3, 18 ));
}
Public int countspecialnumbers (string [] field, int A, int B ){
Int COUNT = 0;
For (INT r = 0; r <field. length; r ++ ){
For (int c = 0; C <field [R]. Length (); C ++ ){
Int temp = getcount (field, R, C );
If (temp> = A & temp <= B ){
Count ++;
}
}
}
Return count;
}

Public int getcount (string [] field, int R, int c ){
Int maxr = field. length;
Int maxc = field [0]. Length ();
Int number = 0;
If (R-1> = 0 ){
Number + = getrc (field, R-1, C );
If (c-1> = 0 ){
Number + = getrc (field, R-1, C-1 );
}
If (C + 1 <maxc ){
Number + = getrc (field, R-1, C + 1 );
}
}
If (R + 1 <maxr ){
Number + = getrc (field, R + 1, C );

If (c-1> = 0 ){
Number + = getrc (field, R + 1, C-1 );
}
If (C + 1 <maxc ){
Number + = getrc (field, R + 1, C + 1 );
}
}
If (c-1> = 0 ){
Number + = getrc (field, R, C-1 );
}
If (C + 1 <maxc ){
Number + = getrc (field, R, C + 1 );
}

Return number;
}

Public int getrc (string [] field, int R, int c ){
Return field [R]. charat (c)-'0 ';
}
}

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.