Locker
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 883 Accepted Submission (s): 374
Problem Description
A password locker with N digits, each digit can be rotated to 0-9 circularly.
You can rotate 1-3 consecutive digits up or down in one step.
For examples:
567890-> 567901 (by rotating the last 3 digits up)
000000-> 000900 (by rotating the 4th digit down)
Given the current state and the secret password, what is the minimum amount of steps you have to rotate the locker in order to get from current state to the secret password?
Input
Multiple (less than 50) cases, process to EOF.
For each case, two strings with equal length (≤1000) consists of only digits are given, representing the current state and the secret password, respectively.
Output
For each case, output one integer, the minimum amount of steps from the current state to the secret password.
Sample Input
111111 222222
896521 183995
Sample Output
2
12
Source
2012 Asia Tianjin Regional Contest
Dp [I] [j] [k] indicates that the first I has been completely matched. At this time, the I + 1 has been added with j bits, k has been added for the I + 2 bits.
Transfer is divided into two steps: enumeration plus, enumeration Subtraction
Note: If the I-th vertex adds a, the I + 1-th vertex adds B, and the I + 2-th vertex adds c, the relation a> = B> = c cannot be wrong.
# Include <stdio. h> # include <string. h> # define inf 1000000000; int dp [1005] [10] [10]; int min (int x, int y) {return x <y? X: y;} int main () {int I, j, k, l, a, c, B, len; char s1 [1005], s2 [1005]; while (scanf ("% s", s1, s2 )! = EOF) {len = strlen (s1); for (I = 0; I <= len; I ++) for (j = 0; j <10; j ++) for (k = 0; k <10; k ++) dp [I] [j] [k] = inf; dp [0] [0] [0] = 0; for (I = 0; I <len; I ++) for (j = 0; j <10; j ++) for (k = 0; k <10; k ++) {c = (20 + s2 [I]-s1 [I]-j) % 10; // for (a = 0; a <= c; a ++) for (B = 0; B <= a; B ++) {dp [I + 1] [(k +) % 10] [B] = min (dp [I + 1] [(k + a) % 10] [B], dp [I] [j] [k] + c); // a maximum of c steps are required. // When k in the current status reaches the I + 1 status, it reaches the corresponding j position} c = (10-c) % 10; // down for (a = 0; a <= c; a ++) for (B = 0; B <= a; B ++) {dp [I + 1] [(k-a + 10) % 10] [(10-b) % 10] = min (dp [I + 1] [(k-a + 10) % 10] [(10-b) % 10], dp [I] [j] [k] + c);} printf ("% d \ n", dp [len] [0] [0]);} return 0;