TopCoder problem "KingsTour" used in TCHS SRM 46 (Division I Level Three)



Problem Statement

    Chess is a game played on a 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. Another piece is the king. A king can move or attack one square in any direction vertically, horizontally or diagonally. When a pawn or king attacks a square, it moves to that square and captures the piece occupying that square.



You are given Strings king, pawnA and pawnB. king is the starting position of a king, pawnA is the starting position of a pawn A, and pawnB is the starting position of another pawn B. Each String contains exactly two characters. The first character is the column ('a'-'h') and the second character is the row ('1'-'8'). All three positions will be distinct. The king's starting position will not be threatened by a pawn. Return the minimum number of moves necessary for the king to capture pawn A. The king is not allowed to move to squares threatened by either of the pawns, and it is not allowed to move outside of the board. The king can capture pawn B but does not have to. Neither of the pawns will move.
 

Definition

    
Class:KingsTour
Method:attackPawns
Parameters:String, String, String
Returns:int
Method signature:int attackPawns(String king, String pawnA, String pawnB)
(be sure your method is public)
    
 

Constraints

-king, pawnA and pawnB will each contain exactly 2 characters.
-The first character in king, pawnA and pawnB will each be a lowercase letter between 'a' and 'h', inclusive.
-The second character in king, pawnA and pawnB will each be a digit between '1' and '8', inclusive.
-king, pawnA and pawnB will be distinct.
-The king's starting position will not be threatened by a pawn.
 

Examples

0)
    
"c4"
"e6"
"d5"
Returns: 2
The king has to capture B before A.
1)
    
"c4"
"d5"
"e6"
Returns: 1
2)
    
"g2"
"a8"
"a2"
Returns: 6
3)
    
"g2"
"a7"
"e4"
Returns: 6
4)
    
"a3"
"b1"
"c1"
Returns: 7

Problem url:

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

Problem stats url:

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

Writer:

Rydberg

Testers:

PabloGilberto , brett1479 , Olexiy , ivan_metelsky

Problem categories:

Brute Force, Dynamic Programming, Simple Search, Iteration