3

Suppose you have a maze represented by a graph where each vertex represents a room and edges represent paths between rooms and each edge has a weight denoting the time it takes to go that way. Now here comes the tricky part: suppose each room needs a set of keys for you to enter, and inside each room you can find another set of keys. Keys can be repeated and one key may be needed to enter on more than one room. You can only enter the room if you have all keys required.

You have an arbitrarily large number of chests with gold inside and each chest needs one key which can be found in the maze. You already have a set of keys that you can use. You can use a key more than once. The question is: how do you collect the keys you need as fast as possible?

2 Answers2

2

If the number of keys is unlimited, the problem is NP-hard, by a straightforward reduction from the traveling salesman problem: simply put a different key in each vertex, and have a single chest that requires all the keys.

D.W.
  • 167,959
  • 22
  • 232
  • 500
0

If the number of keys is a constant, there is a polynomial time solution, simple shortest path graph algorithm.

The trick is to duplicate the maze $2^k$ times, where $k$ is the number of keys - once for each possible subset of keys. Then in each copy of the maze remove all the edges for which you do not have the sufficient keys in that copy.

Finally, for each place that gives you a key make an edge between the two duplicated places, from the graph without the key to the one with the key.

orlp
  • 13,988
  • 1
  • 26
  • 41