(Note: This answer is a work in progress. See relevant meta discussion if you would rather answers such as this did not exist.)
I don't like complicated things so I immediately chose to solve some simple variants of this problem before scaling up to the full deal. The simplest variant adds these assumptions:
- No more than one house can occupy the same place
- All houses are located on a straight line
- The castle is located on the "end" of the line
- There is no size restriction for the kingdom
This is a trivial variant of the problem and the solution is to send the original solider along the line until it reaches the last house on the line. Every other solider immediately returns to the castle. The time taken will be $t = 2x$ where $x$ is the distance between the castle and the house farthest away.
The next variant follows these assumptions instead:
- No more than one house can occupy the same place
- All houses are located on a straight line
- The castle can be located anywhere on the line
- There is no size restriction for the kingdom
To start digging for patterns, I used the following example (X
is the castle; other letters are houses):
A---B-X--C
The optimal strategy will always be:
- Send Solider 1 ($SX$) to $B$, generating Solider 2 ($SB$)
- Send $SX$ to $A$, send $SB$ to $C$
- Send all soldiers home
The cost for this will be:
- $\overline{BX} + \max(\overline{AB}+\overline{AB}+\overline{BX}, \overline{BX}+\overline{CX}+\overline{CX})$
- $= \overline{BX} + \max(2*\overline{AB}+\overline{BX}, \overline{BX}+2*\overline{CX})$
- $= 2*\overline{BX} + \max(2*\overline{AB}+\overline{BX}, \overline{BX}+2*\overline{CX})$
- $= 2*(\overline{BX} + \max(\overline{AB}, \overline{CX}))$
The second example for this variant is:
A---B-X--C--D
The optimal strategy is no longer static and requires knowledge about the distance between the various houses. Specifically, there are two potential options.
OPTION 1
- $S_X$ to $B$
- $S_X$ to $A$, $S_B$ to $C$
- $S_X$, $S_A$, $S_C$ to X, $S_B$ to $D$
- Send all soldiers home
OPTION 2
- $S_X$ to $C$
- $S_X$ to $D$, $S_C$ to $B$
- $S_X$, $S_D$, $S_B$ to X, $S_C$ to $A$
- Send all soldiers home
Therefore, the optimal time will be:
$\min(\overline{XB}+\max(2*\overline{AB}+\overline{XB},\overline{XB}+2*\overline{XC}+2*\overline{CD}), \overline{XC}+\max(2*\overline{CD}+\overline{XC},\overline{XC}+2*\overline{XB}+2*\overline{AB}))$
$= \min(\max(2*\overline{AB}+2*\overline{XB},2*\overline{XB}+2*\overline{XC}+2*\overline{CD}), \max(2*\overline{CD}+2*\overline{XC},2*\overline{XC}+2*\overline{XB}+2*\overline{AB}))$
$= 2*\min(\max(\overline{AB}+\overline{XB},\overline{XB}+\overline{XC}+\overline{CD}), \max(\overline{CD}+\overline{XC},\overline{XC}+\overline{XB}+\overline{AB}))$
$= 2*\min(-\min(-\overline{AB}-\overline{XB},-\overline{XB}-\overline{XC}-\overline{CD}), -\min(-\overline{CD}-\overline{XC},-\overline{XC}-\overline{XB}-\overline{AB}))$
$= 2*(\min(\min(-\overline{CD}-\overline{XC},-\overline{XC}-\overline{XB}-\overline{AB}), \min(-\overline{AB}-\overline{XB},-\overline{XB}-\overline{XC}-\overline{CD}))-\min(-\overline{AB}-\overline{XB},-\overline{XB}-\overline{XC}-\overline{CD})-\min(-\overline{CD}-\overline{XC},-\overline{XC}-\overline{XB}-\overline{AB}))$
$= 2*(\min(-\overline{CD}-\overline{XC},-\overline{XC}-\overline{XB}-\overline{AB}, -\overline{AB}-\overline{XB},-\overline{XB}-\overline{XC}-\overline{CD})-\min(-\overline{AB}-\overline{XB},-\overline{XB}-\overline{XC}-\overline{CD})-\min(-\overline{CD}-\overline{XC},-\overline{XC}-\overline{XB}-\overline{AB}))$
$= 2*(\min(-\overline{XC}-\overline{XB}-\overline{AB},-\overline{XB}-\overline{XC}-\overline{CD})-\min(-\overline{AB}-\overline{XB},-\overline{XB}-\overline{XC}-\overline{CD})-\min(-\overline{CD}-\overline{XC},-\overline{XC}-\overline{XB}-\overline{AB}))$
$= 2*(-\max(\overline{XC}+\overline{XB}+\overline{AB},\overline{XB}+\overline{XC}+\overline{CD})+\max(\overline{AB}+\overline{XB},\overline{XB}+\overline{XC}+\overline{CD})+\max(\overline{CD}+\overline{XC},\overline{XC}+\overline{XB}+\overline{AB}))$
$= 2*(\overline{XB}+\overline{XC}+\max(\overline{AB},\overline{XC}+\overline{CD})+\max(\overline{CD},\overline{XB}+\overline{AB})-\overline{XC}-\overline{XB}-\max(\overline{AB},\overline{CD}))$
$= 2*(\max(\overline{AB},\overline{XC}+\overline{CD})+\max(\overline{CD},\overline{XB}+\overline{AB})-\max(\overline{AB},\overline{CD}))$
Using some quick numbers:
$\overline{AB}=3, \overline{BX}=1, \overline{XC}=2, \overline{CD}=2$ then:
- $t=2*(\max(3,2+2)+\max(2,1+3)-\max(3,2))$
- $t=2*(4+4-3)=2*5=10$
Presumably this pattern can be extended to apply to all scenarios where the houses fit on a line. The second example for this variant is:
A---B-X--C--D---E
Similarly to the second example, we really only have two potential options:
OPTION 1
- $S_X$ to $B$
- $S_X$ to $A$, $S_B$ to $C$
- $S_X$, $S_A$, $S_C$ to X, $S_B$ to $D$
- $S_B$ to $E$
- Send all soldiers home
OPTION 2
- $S_X$ to $C$
- $S_X$ to $D$, $S_C$ to $B$
- $S_D$, $S_B$ to X, $S_X$ to $E$, $S_C$ to $A$
- Send all soldiers home
$t = \min(\overline{XB}+\max(\overline{XB}+2*\overline{AB},\overline{XB}+2*\overline{XC}+2*\overline{CD}+2*DE), \overline{XC}+\max(\overline{XC}+2*\overline{CD}+2*DE,\overline{XC}+2*\overline{XB}+2*\overline{AB}))$
If we treat $2*\overline{CD}+2*DE$ as $2*CE$ we get this form:
$t = \min(\overline{XB}+\max(\overline{XB}+2*\overline{AB},\overline{XB}+2*\overline{XC}+2*CE), \overline{XC}+\max(\overline{XC}+2*CE,\overline{XC}+2*\overline{XB}+2*\overline{AB}))$
Which matches our previous formula letting us jump to the reduced form:
$= 2*(\max(\overline{AB},\overline{XC}+CE)+\max(CE,\overline{XB}+\overline{AB})-\max(\overline{AB},CE))$
$= 2*(\max(\overline{AB},\overline{XC}+\overline{CD}+CE)+\max(\overline{CD}+CE,\overline{XB}+\overline{AB})-\max(\overline{AB},\overline{CD}+CE))$
At this point, one pattern does reveal itself. The only lines that really matter are $XA = \overline{XB}+\overline{AB}$, $XE = \overline{XC}+\overline{CD}+DE$, $\overline{XB}$ and $\overline{XC}$:
$= 2*(\max(\overline{AB},XE)+\max(\overline{CD}+CE,XA)-\max(\overline{AB},\overline{CD}+CE))$
$= 2*(\max(XA-\overline{XB},XE)+\max(XE-\overline{XC},XA)-\max(XA-\overline{XB},XE-\overline{XC}))$
This form seems likely to apply to all scenarios where the houses fit on a line. If we redefine the points in our example:
A1---A0-X--B0--B1---B2
We can write the formula as:
$= 2*(\max(XA-XA_0,\overline{XB})+\max(\overline{XB}-\overline{XB}_0,XA)-\max(XA-XA_0,\overline{XB}-\overline{XB}_0))$ where $XA=XA_0+A_0A_1+A_1A_2...$ and $\overline{XB}=\overline{XB}_0+B_0B_1+B_1B_2...$
This solution makes intuitive sense. The only real choice in a lined scenario is which direction to go first. Everything after that presents us with the trivial problem noted at the beginning of this post. The primary reason this formula works, however, is because we know that each $XA_0 ... XA_N$ is less than $B_0A_0 ... B_0A_N$ since any solider travelling from $B_0$ to $A_0$ has to pass through $X$. Therefore, this formula is only true if and only if $X$ perfectly splits two sections of the kingdom such that $X$ is always closer to each house in section $A$ compared to each house in section $B$ and the houses in each section form a line.
This last clause seems like a good place to extend our approach.
The next variant follows these assumptions instead:
- No more than one house can occupy the same place
- All houses exist in either section A or section B such that there is no line between a house in section A and a house in section B that is shorter than travelling from that house to the castle
- All house in a given section $S$ follow the rule that $S_N$ is closer to $S_{N-1}$ and $S_{N+1}$ than any other houses or the castle unless the house is $S_0$
- The castle can be located anywhere on the line
- There is no size restriction for the kingdom
This set of assumptions removes the restriction that all houses exist on a line. It should make the math significantly more complicated but let's work it through.
A1 A0
X B0
This example moves $A_0$ and $A_1$ off of a line but preserves Assumption 3 which states that $A_1$ cannot be closer to $X$ than $A_0$. Let's see how this affects the math. The strategy is still the same:
- Send $S_X$ to $A_0$
- Send $S_X$ to $A_1$, send $SA_0$ to $S_{B_0}$
- Send all soldiers home
The two major differences are that $\overline{A_1X}$ no longer passes through $A_0$ and $\overline{A_0B_0}$ no longer passes through $X$. Here is our new formula:
- $\overline{XA_0} + \max(\overline{A_0A_1}+\overline{XA_1}, \overline{A_0B_0}+\overline{XB_0})$
Sadly, we don't get to reduce this at all since we don't get to replace $\overline{A_0B_0}$ with $\overline{XA_0} + \overline{XB_0}$. Let's extend section $A$ again and see what happens:
A1 A0
X B0
A2
Is the optimal strategy still the same?
- Send $S_X$ to $A_0$
- Send $S_X$ to $A_1$, send $S_{A_0}$ to $B_0$
- Send $S_X$ to $A_2$, send $S_{A_0}$, $S_{A_1}$, $S_{B_0}$ home
- Send all soldiers home
I can see two alternative strategies and it may be tricky to determine if there is a case where one of those strategies outperforms this one. To make this easier to understand, I'll start using the notation $S_X \to A_0 \to A_1 \to A_2 \to X$ to describe one soldier's entire cycle. The above strategy expressed this way:
OPTION 1
- $S_X \to A_0 \to A_1 \to A_2 \to X$
- $S_{A_0} \to {B_0} \to X$
- $S_{A_1} \to X$
- $S_{A_2} \to X$
- $S_{B_0} \to X$
OPTION 2
- $S_X \to A_0 \to A_2 \to A_1 \to X$
- $S_{A_0} \to {B_0} \to X$
- $S_{A_1} \to X$
- $S_{A_2} \to X$
- $S_{B_0} \to X$
OPTION 3
- $S_X \to A_1 \to A_2 \to X$
- $S_{A_1} \to A_0 \to B_0 \to X$
- $S_{A_0} \to X$
- $S_{A_2} \to X$
- $S_{B_0} \to X$
How do we confirm that OPTIONS 2 & 3 are suboptimal for all positions of $A_0...A_N$?