I was trying to solve the King of the North problem in Kattis. Basically, the problem is, the king has a castle to protect. To do this, he wants to place bannermen across the open areas surrounding the castle (some parts are closed thanks to natural barriers).

A more detailed description of the problem: Given a grid of $R$ rows and $C$ columns, where one square contains our castle and some contain walls. We can place bannermen, who can blockade a square. Each open square has a different amount of bannermen required to form a blockade, which is given. Our task is to determine the minimum amount of bannermen that have to be fielded such that the castle is unreachable from the edge of the grid. (walls and blockaded squares are impassable)

Please go through the attached link for an even more detailed (and colourful) description of the problem.

My idea to solve this:

  1. Go to each outermost corner of the map. From each of these points, do a A* search. This will give the shortest path from the point to the castle.

  2. For each such point on the shortest path, try to see which ones are 'critical' - meaning placing bannermen on those points will force the enemy to take a longer path.

  3. Among all such critical points, see which ones need lesser bannermen.

Anyone has a better idea?

I do not like my approach, particularly because it involves heuristics, and I am not sure whether it is correct.


1 Answer 1


This appears to be a max-flow/min-cut problem. Consider the directed graph $G$ with all non-hill/wall squares as vertices and edges between the neighbouring non-hill/wall cells in the map.

As the weights in the problem are on the vertices, but we need them as capacities on the edges, replace each vertex $u$ by the pair of vertices $u_{in}$, $u_{out}$ such that $c(u_{in},u_{out}) = w(u)$ and all incoming edges of $u$ are connected to $u_{in}$ and all outgoing edges of $u$ are connected to $u_{out}$. Give the capacity $+\infty$ to all other edges (so we cannot 'cut' those edges).

Call our castle the sink and connect all borders of the map to a source. Note that the edges can be bidirectional, except for those connected to the source or sink.

Now, the minimum amount of bannermen to required to protect the castle is equal to the minimum value over edges in our graph $G$ we need to remove such that the sink (our castle) cannot be reached from the source (the outside of the map). This is precisely the minimum cut on this graph $G$. So, we can compute the number of men required in $O((R+C)^3)$ using the Edmonds-Karp algorithm, where $R,C$ is the amount of rows and columns in our map. Given that $R,C\leq 300$, this approach is likely fast enough to solve the problem within the time limit.

Your approach looks a lot like an heuristic approach to the min-cut problem. I don't see any reason that this would be always be correct in this case, so that approach would likely fail, as you suspect.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.