You are faced with the difficult task to set up a dungeon for adventurers. However you made a deal with the guild: any adventurer brave enough to beat the dungeon and reach the treasure room will be guaranteed to recieve some treasure, whatever he or she does. Of course, this goes against your master's rules, and so you will need to be clever about it. Under normal circumstances, the treasure room works as follows:
- There are N keys, and M rooms containing one treasure chest each. Every key opens at least one chest, and every chest has at least one key opening it, but you have no control over which keys open which chests.
- Each adventurer can pick a key, enter one chest room, and attempt to open each chest reachable from that room, with each key in his or her possession.
Your plan is to place the keys on keyrings (so that an adventurer that picks a key also gets any other keys on the same keyring), and dig tunnels connecting different chest rooms (so that an adventurer that enters a chest room can also reach any other rooms connected to it via tunnels), so that for every combination of keyring and chest room, there is at least one key on the keyring that can open a chest reachable from that room. Now, if you were unsupervised, you could put all N keys on the same keyring and connect all M rooms to each other, but you are under close surveillance, so you will need to minimize the risk of getting caught. In practice, this means that you will need to ensure the adventurer wins, with the fewest combinations of keys and chest. For example, combining key 1 and key 2 in one key ring and then combining this key ring with key 3 counts for 2 combinations, and the same goes for chests. You don't have all eternity to find a solution. Your procedure should be able to tell you the actions to take in a reasonable time as a function of N and M.
Is there a way you can ensure you fulfill your contract in the minimum number of combinations?