USACO Training "Camelot"
USACO Training: "Camelot" (This was a hard problem) Problem Analysis If the problem did not specify that the king needed to be picked up, this would be a simple all-pairs shortest pathes problem: find the point (r,c) such that sum(distance((r,c),(i-th knight row, i-th knight column))) is minimized. However, we need to take into account that the king needs to be picked up and ridden to (r,c) too! Our first discovery: The king can only take at most two steps. Why? We notice that for any sequence of three (non-undoing) steps the king makes, the knight can do it in three (or sometimes even two) steps, so it would be inconstructive for a king to move three steps when a knight carrying it can. We try this method: Choose an arbitrary point (i,j). Add up all the distances between each knight and (i,j) - if no shortest path exsists, (i,j) is invalid. For each position a king can be in with at most two moves (ki, kj) find i such that the value: max(abs(ki-kr), abs(kj-k