12
1
There are n boxes, numbered 1-n. Each box is locked, such that it can be opened by only one corresponding type of key (also numbered 1-n). These keys are randomly scattered in the boxes (one box may have any number of keys, one key may have any number of duplicates), and then all boxes are shut. A treasure (numbered 0) has also been locked in many of the boxes.
You have hired a locksmith to retrieve all the treasure. He charges for each box he cracks open. There is no charge for opening a box for which the key is already available.
Input is the contents of each box. You can decide the format of the input.
Output the minimum cost required to get the treasures.
Notes
- Your algorithm may take a long time, but that is irrelevant.
- Shortest code wins.
- No need to bother about invalid input.
Sample data
Here line i represents the keys present in box i.
Input
2 0
3
4 0
5 6 0
6
0
Output
1
Input
2 0
3 0
4 0
6
5 0
Output
3
Input
2 4 0
3 0
1 0
6
5 0
Output
2
Input
1
3 4
2 6
5
Output
0
2
Is this perhaps related to this?
– Addison Crump – 2015-11-19T15:51:58.647Also related: http://puzzling.stackexchange.com/questions/23150/how-to-beat-count-dracula
– Leif Willerts – 2015-11-19T16:10:58.473@VoteToClose Nice video. It is similar, except that it talks of a mathematical puzzle and specific algorithm, rather than a generalised one. – ghosts_in_the_code – 2015-11-19T17:47:12.090
@VoteToClose, not really. That question is about permutations; this question is graph reachability plus smallest covering set. – Peter Taylor – 2015-11-19T18:55:16.693
1
This seems related to this puzzle about 100 locked boxes of wood and steel: http://puzzling.stackexchange.com/q/17852/4551
– xnor – 2015-11-20T04:31:04.793I'm not a fan of the specific input format. I expect I'd need a decent fraction of my code just to put it in a usable form. In the future, I suggest allowing more flexible forms of input for algorithmic style problems. – xnor – 2015-11-20T04:34:33.437
@xnor The input is simple only. How can I make it simpler than this? – ghosts_in_the_code – 2015-11-20T10:30:57.203
4@ghosts_in_the_code It's not about simplicity but about flexibility. Commonly, challenges that require structured input allow any convenient list format, as long as the data is not preprocessed. Depending on the language that could mean a whitespace separated file like you have, or it could mean
[[1] [3 4] [] [] [2 6] [5]]or maybe{{1},{3,4},{},{},{2,6},{5}}. This way, most languages can reduce reading the input to something as trivial asi=eval(read())and focus on the fun part of the challenge. – Martin Ender – 2015-11-20T16:55:04.570I am missing something to understand this puzzle. How does the last one work to zero if we don't start with any keys and locksmith charges to open a box we don't already have the key for? – ŽaMan – 2015-11-20T23:22:29.453
@qumonio Only all the treasure (0s) need to be found. Since there are no 0s, we have already completed the challenge even before opening any box. – ghosts_in_the_code – 2015-11-21T07:30:18.713
@xnor @ MartinButtner Done. – ghosts_in_the_code – 2015-11-21T07:34:39.993