← 返回 uber 的题目列表Minimum Moves to Unlock a Lock Combination
类型:online_judge
You are given a digit lock whose state is a string of length L (each character is 0-9). You start from start and want to reach target.
In one move, you may choose an index i and rotate that digit one step up or down (9 wraps to 0, 0 wraps to 9). Some states are forbidden (deadends). If you enter a deadend state, you cannot continue.
Return the minimum number of moves to reach target from start, or -1 if impossible.
Input
start: string of length L with digits 0-9
target: string of length L with digits 0-9
deadends: array of strings, each of length L
Output
minimum moves (int), or -1
Constraints
1 <= L <= 10
0 <= len(deadends) <= 1e5
Examples
start="0000", target="0202", deadends=["0201","0101","0102","1212","2002"] -> 6
start="0000", target="0000", deadends=[] -> 0
start="0000", target="8888", deadends=["0000"] -> -1
start="000", target="001", deadends=["009"] -> 1
start="9", target="0", deadends=[] -> 1
Example
Input
0000
0202
5
0201
0101
0102
1212
2002
Output
6