This is another very interesting problem that can be solved using graph search. The problem itself looks
like an optimization problem and should be solvable using ‘traditional’ dynamic programming technique (which
is true), but with some observations we can turn this into a graph search problem:
- The problem can be reduced to a search problem.
- The search space is the set of states where each state is a combination of ‘boss defeated so far’ and ‘shots used’.
- For each boss, the state of ‘defeated’ equals to the presence of its weapon.
- The state transfer function looks like: given the set of boss defeated and the shots taken, what would be the set of bosses
that we could defeat next and the corresponding shots we would use to defeat those boss? - So we represent each state as a node in a graph and the state transfer function as edges between graph nodes.
- The problem now is reduced to find shortest path between two nodes: the start node where no boss is defeated and the end
node where all bosses have been defeated.
typedef struct GNode { |