Dial Lock
Time limit2sMemory limit128 MB
Given a current dial state and a password of N digits, find the minimum number of operations, each rotating a contiguous block of at most three circular dials by 1 to 3 steps, to reach the password.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Math, Greedy, Array
- Solved
- No attempts yet
Problem
A laptop is protected with a dial lock. The lock consists of circular disks. Each disk shows one digit from 0 to 9, and because each disk is circular, 0 and 9 are adjacent.
In one operation, choose a non-empty contiguous block of at most three disks. Rotate every chosen disk in the same direction by the same number of steps, where the number of steps is between 1 and 3. The direction may be clockwise or counterclockwise.
Given the current state of the lock and the password, find the minimum number of operations needed to open the lock.
For the state 555 and password 464, rotating each disk separately would take 3 operations. Instead, rotate all three disks one step counterclockwise to make 444, then rotate the second disk two steps clockwise to make 464, for a total of 2 operations.
Input
The first line contains , the length of the password and the number of disks in the lock. is a positive integer at most 100.
The second line contains the current state of the lock, and the third line contains the password. Both strings have length and consist only of digits from 0 to 9.
Output
Print the minimum number of operations needed to open the lock.