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
Definition
- Class:
- CollectingRiders
- Method:
- minimalMoves
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int minimalMoves(String[] board)
- (be sure your method is public)
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
{"...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......", "........", "....3...", "........", "........", ".7......", "........"}
Returns: 2
In 2 moves, we can move all the pieces to the cell initially occupied by the 1-rider.
{"..", "2.", ".."}
Returns: 0
No moves are necessary.
{".1....1."}
Returns: -1
{"9133632343", "5286698232", "8329333369", "5425579782", "4465864375", "8192124686", "3191624314", "5198496853", "1638163997", "6457337215"}
Returns: 121
Kind of maximal test.
{"1..", ".1.", "..."}
Returns: -1
impossible
{"1...1", "....."}
Returns: 2
{"1111111111", "1111111111", "1111111111", "1111111111", "1111111111", "1111111111", "1111111111", "1111111111", "1111111111", "1111111111"}
Returns: 266
max answer
{"..45.....5",".......5..",".........2","3.9......1",".....2....","..1.......","......68..","....1..9..","..........","......5..7"}
Returns: 19
{"..........","1....4.7..","9.2.......","....3.9...","......2.4.","3......9..","......8...",".....7..7.","...8....5.",".........."}
Returns: 15
{".1.....2..","6.8......8","....6.7...","..........","9.........","....16....","....1.....","..7.....2.","..86......","...1......"}
Returns: 21
{".........5","3.........",".....2.578","..4...1...",".....72...",".2........","...41....9",".........5","64..9..499","...4.....9"}
Returns: 24
{".........2",".16.1..6..","........34",".....6....","..21.3....","2......9..","78........","23.4......",".....7....","4........."}
Returns: 24
{".....31396",".37.......","..6.....8.",".8........","........3.","......3..8",".........2",".......4..","...9....7.","...17.4..."}
Returns: 24
{"281..5....",".99.....8.","....26.5..","598....1..","....6.....","........9.","...9.2...6","..2.......","..21...8..",".4..1.6..."}
Returns: 33
{"......7...","...49...5.","..........",".7........","..19.9....","..........",".43.572.9.","....3.....","6.........","2....54.1."}
Returns: 22
{"..........","..9..9....","..........","4.3.......","......7...","...1.3....","....2.6..2","7......9.8","..........","..7.4....2"}
Returns: 17
{"...7.7....",".5........","..6..63...","....9.....",".4...6....","..8....5..","6..6.9...7","....91.6.6","...6.7..28",".........."}
Returns: 23
{"213.9.3.87","..8.1.2..4","..5.8.81.2","9.3..2.2..","...175.4..",".1.....65.",".5.1...221","....4...86",".7367.1...","..74....8."}
Returns: 55
{"7.82.27..5","..9.735.56","..924..9..","....86744.",".94.47.6..","13.8...145",".7137.7431",".3.8664...","355247.76.","5...538..3"}
Returns: 65
{".7546.58.8","27.7..5.7.","6..1....8.","33.72.5.83","..1..3.42.","5978998.68","...1..99..",".....9.7..","...2.635.6",".23558.8.1"}
Returns: 58
{"73...971..","1..888....",".6532.2..4","...4....24","..71.27.6.","......3...","..6...6333","8377..919.","2.99276..7","...15.4768"}
Returns: 56
{"162....973","659....2.1","7....2.68.","1.447973.4","7.49472..7",".26.55.8..","31..496.3.","3.....4539",".8.6....65","39.4..1.37"}
Returns: 67
{"241.6.21..",".1.8422...",".11...47..",".....13...",".542.5..19","73..86.6.1","86.5..8.8.","88..33...2",".6..7...6.","..6....89."}
Returns: 55
{"4652.1..1.","3.6...8.16",".1852.2..7","3.5..73515","467.1716..",".7338895..",".54458.4..","....14..48","4...4.7..3","..24...7.4"}
Returns: 68
{"99246.938.","7.1574..26","...1.2..8.","..1..67.4.","27..585499","...9298...","4518.4.6.6","4.52...64.","..7.1..214",".634.85748"}
Returns: 70
{"13794654.3",".5..8....5",".4165.7239","...571...2","1..7.326..","3.9.....23","....556.6.",".7.1.5.67.","5.7.3.272.",".....7...8"}
Returns: 59
{"4..48.1...","2..276...5","....5.38.5",".4165.63..","1.44..18.2",".12.8.5..4","..8.4.6752","..........","2.3.8....3","56.1..75.6"}
Returns: 57
{"15547.4317","439872379.",".782452356","3968.92516","1652562..8","3585131598","68141..218","7599185894",".327957657","3518387255"}
Returns: 111
{"3996234424","4442382957","2.6.1.6327","728.679314","1647466885","9.5.572729",".239875365","3543553254","7333413824","97.2773..8"}
Returns: 103
{"5793558.43","42.7259878","33.2554566","86569843.3","7745263213","2.8..45715","177167.5.4","5344981581","9853.57573","14.4613.26"}
Returns: 104
{"8237388739","86.9123298","9136821564","3.2.8.4.87","2223443815","4336488723","4893881933","82.27.9363","72236.17.1","6.324..912"}
Returns: 111
{"6879226641","7.2177511.",".3387679.4","1.4866673.","4266184392","..85531144","499.436423","1976252338","..81216952","864413.322"}
Returns: 115
{"1313425.8.","127514713.","8464478343","61.9112319","26.5381138","5996564485","6418..7597","1467249846","916.71.63.","5713.12355"}
Returns: 116
{"679695.26.","4636447929","2685.22531","4965253539","2297915622","3653426524","633.91.385","579.646697","36447851.6","549378.179"}
Returns: 102
{"27885.4823","4273918.36","3466117894","9295268811","974596897.","3348976659","53994662.1","6997.53193","6.64479139","3.36765344"}
Returns: 109
{"3775657264","7285935211","1237.25542","7889337885","65.7455494","5.2.534946","9216291322","8325399592","5633212646","6856953597"}
Returns: 111
{"811533.1.4","8612211243","3828699715","6845791.4.","8756427734","398..21672","2983968918","8518958654","7843167.78","1324236265"}
Returns: 119
{"..........","......1...","....1.....","......1..1","..........","..........",".....1....","..........","..........",".....1...."}
Returns: 11
{"........11","..........","1.........","..........",".1........","..........","..........","..........","..........","...1......"}
Returns: 13
{"..........","..........","..........","..........","..........","..........","..........","..........","........1.",".........."}
Returns: 0
{"..........","......1...","..........","..........","....1.....","..........","..........","...1....1.","..........",".........."}
Returns: 8
{"..........","1.........","..........","..........","..........","..........","......1...","..........","..........","1........."}
Returns: 7
{"..........","..........","..........","1.........","..........","..1..1....","..........",".....1....","..........",".........."}
Returns: 6
{"..........","..........","......1...","...1......","..........","..........","..........","..1.......","..........","....1....."}
Returns: 8
{"..........","..1.......","1.........","....1....1","........1.","....1.....",".......1..","..........",".....1....",".........."}
Returns: 17
{"...1......","......1...","..........","..........","..........","..........",".........1","..........",".......1.1","......1..."}
Returns: 14
{"..........","..........","11........","..........","..........",".........1","..........","..........","..........",".......1.."}
Returns: 11
{"..........",".2....2...","......2...","1.........","...2.1....","...1......","..........",".......21.","..........",".........."}
Returns: 14
{"...3.1.3..","....2.321.","..2...3...","..32......",".......1..","...13.....","..........","......2.3.","..21..32..","......3..."}
Returns: 28
{"..1.......","...1......","....1.....","3......2..",".2........","..........",".....1....",".3.....2..","2......1..",".1..3....."}
Returns: 22
{"..2.1.1...","2..3...1..","..........","........3.","3......1..",".....2....","2........1",".....1.3..","..........","......2.1."}
Returns: 28
{"...3......",".1..32...2","..2.......","...1.12...","2..1.2.2.1",".......321","1.......1.",".....1....",".3...2....",".1...1...."}
Returns: 44
{"..........","..3....1..","..3.1.....","1.3.3.....","..........","..........","3......1..","....2...3.",".....2....",".........."}
Returns: 17
{"..........","...2......","........1.",".....1..1.","3......313",".2...2....",".........1","..2...2.13","..........","....13...."}
Returns: 28
{".......3..",".2...2....",".3...3...3",".....3....",".3.....3..",".....1....",".11.....3.","..1...2.1.","........2.","...1......"}
Returns: 26
{".....3.1.3","...22..12.","........22","1..3......","3........2",".....3....","1.........","..........","..........","..2..2...."}
Returns: 28
{"3.........","........3.","2.2.....2.",".....3....","....3....1",".3.....33.","...1......",".........3","..2....3..","..12.2...."}
Returns: 24
{"1.", "..", "..", "..", "1."}
Returns: 2
{"1.", "..", "..", "..", "..", "1."}
Returns: -1
{"1....1", "......"}
Returns: -1
{"........", "...1.1..", "........", "........", "........", "...1.1..", "........", "........"}
Returns: 4
{"9133632343", "5286698232", "8329333369", "5425579782", "4465864375", "8192124686", "3191624314", "5198496853", "1638163997", "6457337215" }
Returns: 121
{"...", ".99", "..." }
Returns: -1
{"232..11123", "323.323232", "23.1232323", "3232323232", "121.12121.", "3232323232", "1111653278", "21326.5487", "1914181613", "2323232323" }
Returns: 139
{".73363234.", "522367.654", "8323243369", "54..576782", ".578564375", "8194364686", "3129865557", "5134754353", "1356733997", ".87654551." }
Returns: 103
{"1...1", ".....", ".....", ".1.1.", "....." }
Returns: 4
{".1111" }
Returns: -1
{"123", "4.5", "678" }
Returns: 7
{"..........", ".........1", "..........", "..........", "..........", "..........", "..........", "..........", "..........", "9........." }
Returns: 1
{"1..", "...", "..1" }
Returns: 4
{"1..", ".1.", "..1" }
Returns: -1
{"..1..", "1...1" }
Returns: 2
{"..", "29", ".." }
Returns: -1
{"1111111111", "1111111111", "1111111111", "1111111111", "1111111111", "1111111111", "1111111111", "1111111111", "1111111111", "1111111111" }
Returns: 266
{"1111111111", "1111111111", "1111111111", "1111111111", "1111111111", "1111111111", "2222222222", "2222222222", "2222222222", "1111111111" }
Returns: 233
{"1.", ".1" }
Returns: -1
{"1....1", "..1..." }
Returns: -1
{"9..9" }
Returns: -1
{"1111" }
Returns: -1
{"99" }
Returns: -1
{"1...1", ".....", "1...1" }
Returns: 4
{"1.1", "...", "..1" }
Returns: 4
{".111" }
Returns: -1
{"999999999", "999999999", "999999999", "999999999", "999999999", "999999999", "999989999", "999999999", "999999991", "999999999" }
Returns: 89