{ "...1", "....", "2..." }The answer is 2: The 2-rider can go from (2,0) to (0,1) to (2,2) as a first move. And then the 1-rider can go from (0,3) to (2,2) on its first move. That's a total of two moves.
{ "........", ".1......", "........", "....3...", "........", "........", ".7......", "........" }The answer is 2: The 3 and 7 riders can both get to (1,1) in one move.
{ "..", "2.", ".." }The answer is 0.
{ ".1....1." }The answer is -1.
{"9133632343", "5286698232", "8329333369", "5425579782", "4465864375", "8192124686", "3191624314", "5198496853", "1638163997", "6457337215"}The answer is 121.
Now, determine the shortest path from the rider in the upper right-hand corner to every other node:
And determine the shortest path from the rider in the lower left-hand corner to every other node:
Recall that this rider can do one or two jumps in every move. That means you can convert those shortest jumps to shortest moves by adding one and dividing by two. Why? Well, consider a node that a 1-rider can reach in two jumps. That means a 2-rider can reach it in one jump. (2+1)/2 = 1. Now, consider a node that a 1-rider can reach in three jumps. That means a 2-rider can reach it in two jumps. (3+1)/2 = 2. See how the math works? You'll have to derive the math for each kind of rider (it's not complex). Here is the graph for the 2-rider:
I'm going to redraw the two graphs here side by side -- the left one is the rider in the upper-right corner, and the right one is the rider in the lower-left corner:
For each node, add the two shortest distances. When you're done, you can see that the minimum node is two (there are two of them, and you can verify by hand that they work):
That gives you a nice roadmap towards solving this problem with BFS. Think about running time. Each BFS is O(|V|+|E|), which is O(r*c). There are up to r*c riders, so you have to do up to r*c BFS'. Therefore, the total is O(r*c*r*c). Since r and c are limited to ten, this is not a problem (and it's why I bumped it up to 30 in tests.sh).
When you're hacking up the BFS, think about how you want to represent the graph. I didn't represent edges explicitly. I simply simulated each jump when I processed a node during the BFS.