TopCoder problem "Pawns" used in TCHS SRM 46 (Division I Level One)



Problem Statement

    Chess is a game played on a square board of eight rows and eight columns of squares. Columns are marked 'a' to 'h' from left to right, and rows are numbered '1' to '8' from bottom to top. To play chess, you have to understand the paths by which pieces can attack. One of the pieces is a pawn. A pawn attacks diagonally, one square upward and to the left or right. For example, if a pawn is on c3, it threatens d4 and b4.



You are given a String[] pawns, each element of which represents the position of a pawn on the board. Each element contains exactly 2 characters. The first character is the column ('a'-'h') and the second character is the row ('1'-'8'). Return the number of empty squares on the board that are threatened by pawns.
 

Definition

    
Class:Pawns
Method:pawnsAttack
Parameters:String[]
Returns:int
Method signature:int pawnsAttack(String[] pawns)
(be sure your method is public)
    
 

Constraints

-pawns will contain between 1 and 20 elements, inclusive.
-Each element in pawns will contain exactly 2 characters.
-The first character in each element of pawns will be a lowercase letter between 'a' and 'h', inclusive.
-The second character in each element of pawns will be a digit between '1' and '8', inclusive.
-Each element in pawns will be unique.
 

Examples

0)
    
{"a1","a8","h8"}
Returns: 1
Watch out for the corners.
1)
    
{"a1","b2","c3","d4","e5","f6","g7","h8"}
Returns: 6
Only 6 of the 13 threatened squares are unoccupied by pawns.
2)
    
{"g7","h8","h6"}
Returns: 1
3)
    
{"c1","c2","c3","c4","c5","c6","c7","c8",
"f1","f2","f3","f4","f5","f6","f7","f8",
"a1","a2","a3","a4"}
Returns: 28

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=8108

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10801&pm=8108

Writer:

Rydberg

Testers:

PabloGilberto , brett1479 , Olexiy , ivan_metelsky

Problem categories:

Brute Force, Simple Search, Iteration