Connection Status:
Competition Arena > CollectingRiders
SRM 382 · 2007-12-11 · by dkorduban · Graph Theory
Class Name: CollectingRiders
Return Type: int
Method Name: minimalMoves
Arg Types: (vector<string>)
Problem Statement

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.

Notes

  • 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.

Constraints

  • 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.
Examples
0)
{"...1",
 "....",
 "2..."}
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.

1)
{"........",
 ".1......",
 "........",
 "....3...",
 "........",
 "........",
 ".7......",
 "........"}
Returns: 2

In 2 moves, we can move all the pieces to the cell initially occupied by the 1-rider.

2)
{"..",
 "2.",
 ".."}
Returns: 0

No moves are necessary.

3)
{".1....1."}
Returns: -1
4)
{"9133632343",
 "5286698232",
 "8329333369",
 "5425579782",
 "4465864375",
 "8192124686",
 "3191624314",
 "5198496853",
 "1638163997",
 "6457337215"}
Returns: 121

Kind of maximal test.

5)
{"1..",
 ".1.",
 "..."}
Returns: -1

impossible

7)
{"1111111111",
 "1111111111",
 "1111111111",
 "1111111111",
 "1111111111",
 "1111111111",
 "1111111111",
 "1111111111",
 "1111111111",
 "1111111111"}
Returns: 266

max answer

Submissions are judged against all 85 archived test cases, of which 7 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class CollectingRiders with a public method int minimalMoves(vector<string> board) · 85 test cases · 2 s / 256 MB per case

Submitting as anonymous