TopCoder problem "PlayerExtraction" used in SRM 374 (Division I Level Two , Division II Level Three)



Problem Statement

    

Three weeks ago, MyOwnBusiness Inc. received an urgent call from the IIHF (International Ice Hockey Federation) requesting a system to raise an alarm to the referee when there are too many players from the same team inside the rink. The system will be composed of three parts:

  1. A digital camera in the ceiling to take photos of the rink every second.
  2. A software module to extract the position of each player from the photo taken by the digital camera.
  3. A software module to count the number of players from the same team inside the hockey rink.

The team has just finished the module to count the number of players inside the hockey rink, so now it is time to implement the module to extract the players' positions from the photo taken by the digital camera.



The photo taken by the camera is given as a String[], where the x-th character of the y-th element is the color of the 2 x 2 square whose lower-left corner is at (2*x, 2*y). Colors are either uppercase letters ('A'-'Z') or digits ('0'-'9'). Uppercase letters represent terrain features (floor, chairs, spectators, etc.) and each digit X represents the color of the uniform used by the X-th team.



Two squares A and B belong to the same object if and only if there exists a chain of squares where the first square is A, the last square is B, each pair of consecutive squares in the chain shares a common edge and all the squares in the chain have the same color. The position of an object C is the center of its bounding box, where its bounding box is defined as the smallest axis-aligned box that contains all the object's squares. An object's area is defined as the sum of the areas of all the squares that compose the object. An object is a player from the i-th team if and only if it is colored with the digit i and its area is at least threshold.



Return a String[] containing all the players in the photo from the k-th team. Each element should represent a single player and be formatted "X Y" (quotes for clarity), where (X, Y) is the center of the player's bounding box, and X and Y have no extra leading zeroes. Sort the players in increasing order by x-coordinate. Sort players with the same x-coordinate in increasing order by y-coordinate.



 

Definition

    
Class:PlayerExtraction
Method:extractPlayers
Parameters:String[], int, int
Returns:String[]
Method signature:String[] extractPlayers(String[] photo, int k, int threshold)
(be sure your method is public)
    
 

Constraints

-photo will contain between 1 and 50 elements, inclusive.
-Each element of photo will contain between 1 and 50 elements, inclusive.
-Each element of photo will contain the same number of characters.
-Each element of photo will contain only uppercase letters ('A'-'Z') and digits ('0'-'9').
-k will be between 0 and 9, inclusive.
-threshold will be between 1 and 10000, inclusive.
 

Examples

0)
    
{"33JUBU33",
 "3U3O4433",
 "O33P44NB",
 "PO3NSDP3",
 "VNDSD333",
 "OIVNFD33"}
3
16
Returns: {"4 5", "13 9", "14 2" }
There are four groups of adjacent pixels with color '3'. However, the first one is too small to be considered (its area is 12, which is smaller than the threshold).
1)
    
{"6VS",
 "D66"}
6
1
Returns: {"1 1", "4 3" }
There are two players from the 6-th team in the image. Remember that diagonal pixels are not considered adjacent.
2)
    
{"44444H44S4",
 "K444K4L444",
 "4LJ44T44XH",
 "444O4VIF44",
 "44C4D4U444",
 "4V4Y4KB4M4",
 "G4W4HP4O4W",
 "4444ZDQ4S4",
 "4BR4Y4A444",
 "4G4V4T4444"}
4
16
Returns: {"3 8", "4 16", "5 4", "16 3", "16 17", "17 9" }
3)
    
{"8D88888J8L8E888",
 "88NKMG8N8E8JI88",
 "888NS8EU88HN8EO",
 "LUQ888A8TH8OIH8",
 "888QJ88R8SG88TY",
 "88ZQV88B88OUZ8O",
 "FQ88WF8Q8GG88B8",
 "8S888HGSB8FT8S8",
 "8MX88D88888T8K8",
 "8S8A88MGVDG8XK8",
 "M88S8B8I8M88J8N",
 "8W88X88ZT8KA8I8",
 "88SQGB8I8J88W88",
 "U88H8NI8CZB88B8",
 "8PK8H8T8888TQR8"}
8
9
Returns: 
{"1 17",
"3 3",
"3 10",
"3 25",
"5 21",
"8 17",
"9 2",
"10 9",
"12 23",
"17 16",
"18 3",
"18 11",
"18 28",
"22 20",
"23 26",
"24 15",
"27 2",
"28 26",
"29 16" }
4)
    
{"11111",
 "1AAA1",
 "1A1A1",
 "1AAA1",
 "11111"}
1
3
Returns: {"5 5", "5 5" }

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10793&pm=8225

Writer:

jbernadas

Testers:

PabloGilberto , Olexiy , Andrew_Lazarev

Problem categories:

Sorting, String Manipulation