There are a 10 buttons. These buttons can unlock the lock if pressed in correct order (5 presses in sequence). Every button press triggers unlock check.
Example: "password" is 123456 and I press buttons 0 1 2 3 4 5 6 I unlock the lock from 6th button press.
I need to design algorithm that tries all possible combinations in the most efficient way (i.e. minimum amount of buttons should be pressed).
I can interpret button number as digit and number of pressed button in sequence as digit position and then try all 99999 combinations in attempt to unlock the lock but I feel that there is a more efficient algorithm to do that.
Is there something I can do to optimize this search?
To optimize a brute-force attack on a lock, you can use De Bruijn sequences.
The sequence can be used to shorten a brute-force attack on a PIN-like code lock that does not have an "enter" key and accepts the last n digits entered. For example, a digital door lock with a 4-digit code would have B(10, 4) solutions, with length 10,000. Therefore, only at most 10,000 + 3 = 10,003 (as the solutions are cyclic) presses are needed to open the lock. Trying all codes separately would require 4 × 10,000 = 40,000 presses.
来源:https://stackoverflow.com/questions/12211425/need-help-in-building-efficient-exhaustive-search-algorithm