TopCoder problem "CollectingRiders" used in SRM 382 (Division I Level One , Division II Level Two)

Problem Statement


A rider is a fantasy chess piece that can jump like a knight several times in a single move. (See notes for a description of how a knight jumps.) A rider that can perform a maximum of K jumps during a single move is denoted as a K-rider. For example, a 2-rider can jump once or twice during a single move, and a 1-rider is a traditional knight.

There are some riders of different types on a chessboard. You are given a String[] board representing the layout of the pieces. The j-th character of the i-th element of board is the content of the square at row i, column j. If the character is a digit K between '1' and '9', the square contains a K-rider. Otherwise, if the character is a '.', the square is empty. Return the minimal total number of moves necessary to move all the riders to the same square. Only one piece can move during each move. Multiple riders can share the same squares at all times during the process. Return -1 if it is impossible.



Method signature:int minimalMoves(String[] board)
(be sure your method is public)


-A traditional knight has up to 8 moves from a square with coordinates (x,y) to squares (x+1,y+2), (x+1,y-2), (x+2,y+1), (x+2,y-1), (x-1,y+2), (x-1,y-2), (x-2,y+1), (x-2,y-1), and can not move outside the chessboard.


-board will contain between 1 and 10 elements, inclusive.
-Each element of board will contain between 1 and 10 characters, inclusive.
-All elements of board will have the same length.
-board will contain only positive digits ('1'-'9') and '.' characters.
-board will contain at least one digit.


Returns: 2
The 2-rider can jump from (2,0) to (0,1) in the first move, and then from (0,1) to (2,2) to (0,3) in the second.
Returns: 2
In 2 moves, we can move all the pieces to the cell initially occupied by the 1-rider.
Returns: 0
No moves are necessary.
Returns: -1
Returns: 121
Kind of maximal test.

Problem url:

Problem stats url:




PabloGilberto , Olexiy , marek.cygan , ivan_metelsky

Problem categories:

Graph Theory