I assume that you know how to do this for one Knigt.
You can reformulate your problem as a linear program:
I will use the following notation:
We have N knights and N en places.
xij = 1
if you chose knight i to go to location j and 0 otherwise
cij
- minimum length of a moving knight i to location j
Then you have the following linear program:
Variables
xij for i j in [0, N]
Cost function:
C = SUM (cij.xij for (i, j) in [0, N] x [0, N])
<strong> limitations:
SUM (xij for j in [1, N]) = 1 // exactly one knigt goes from i to j
SUM (xij for i in [1, N]) = 1
(The matrix (xij) is a stochastic matrix)
if X is a matrix (xij), you have n! possible matrix. This problem may be (there is no simple solution to this system, the system solution is quite similar to testing all possible solutions).
EDIT:
This problem is called the assignment problem and there are many algorithms for solving it in polynomial time. (see @purav example for an example)
As @Purav mentioned, although such problems can be NP-hard, in this case it can be solved in O (n ^ 3)
About the problem @j_random_hacker raised:
Problem
If the knight is at the endpoint, the following knights should not go through this endpoint. Therefore, Cij may need to be updated after each knight moves.
Notes:
1. several optimal ways:
Since there are no restrictions on the side of the chessboard (limited chessboard), the order in which you make your move to achieve the shortest path is irrelevant, therefore there is always a lot of other shortest path (I wonโt combinatorics here).
An example with two knights
Say you have 2 K and 2 endpoints ('x'), the optimal path extends.
-x | | x | K-- --K
you move the right K to the first point (1 step), the second cannot use the optimal path.
-x | | K | K-- --:
But I can easily create a new optimal path, instead of moving 2 from the right 1 up, then 2 up 1 to the right. 1 can move 2 up 1 right 1 up 2 right (just the opposite)
--K | - | K | | : --:
and any combination of paths works:
1 U 2 R, then 2 U 1 R, etc .... while I keep the same number of UP LEFT DOWN and RIGHT moves and that they are valid.
2. Knights moving order:
Secondly, I can choose the order of my movement.
Example:
with the previous example, if I decided to start with the left knight and go to the upper end point, you no longer need to limit the end point.
-K | | x | :-- --K -K | | K | :-- --:
With these two remarks, one could prove that there is no situation where the calculated lower bound is not optimal.