HDU 4433 locker 37th ACM/ICPC Tianjin Division Field Competition C-question (DP)

Source: Internet
Author: User
Locker

Time Limit: 6000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 248 accepted submission (s): 87

Problem descriptiona 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?

 

Inputmultiple (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.

 

Outputfor each case, output one integer, the minimum amount of steps from the current state to the secret password.

 

Sample input111111 222222 896521 183995

 

Sample output2 12

 

Source2012 Asia Tianjin Regional Contest

 

Recommendzhoujiaqi2010 This is equivalent to a digital password lock. You can rotate 1-3 consecutive numbers in the forward or backward direction each time. The minimum number of steps required to change from the current status to the target status. The question gives two strings of the same length consisting of 0-9. This is equivalent to adding 1 or reducing 1 to 1 consecutive numbers in each operation. However, this addition and subtraction is cyclic. 0 minus 1 to 9, 9 plus 1 to 0.   DP. However, the DP equation is hard to think about or express the state.   DP [I] [x] [Y] indicates the minimum number of steps required to process the I-th digit, and the last two digits are X and Y. When calculating DP [I] [x] [Y], the front I-2 bit is the value of the current State given by the question, the I-1 bit is X, and the I bit is y, it is to convert the first I bit to the correct one.   You need to move the DP [I] status to DP [I-1. You can transfer the I-th bit from X to the target State B [I.   There are three types of rotation related to the I-bit: one is the single turn the I-bit, the other is the turn the I-bit and the I-1-bit, the third is to turn the I, the I-1 AND THE I-2. The XX, YY in DP [I-1] [XX] [YY] can be determined based on three types; Rotation is divided into forward and reverse. If the I-th digit is a forward direction, you need to perform Step 1 if the forwarding is correct. So the I-1 and the I-2 is not less than or equal to D1. And the number of steps of the I-2 is less than or equal to the I-1. If the I-th digit is a forward direction, perform step D2 to convert it correctly. So the I-1 and the I-2 is not less than or equal to D2. And the number of steps of the I-2 is less than or equal to the I-1. In this way, I will be transferred from 1 to n during DP. When processing DP [I], DP [1 ~ (N-1)] [0 ~ 9] [0 ~ 9] are known. It is easy to determine DP [I] [0 ~ 9] [0 ~ 9. Note that the initialization process is handled. All initialization tasks are INF, DP [0] [0] [0] = 0; When there is another transfer, I = 1 and I = 2 are processed separately .. Transfer is 1000*100*100 at most. Is acceptable. At the beginning, I added the idea that DP [I] [x] [y] [Z] represents the last three digits. It is easy to understand, but it is TLE. Reduce the value of one dimension to the value of AC. For details, seeCodeRight! The annotations are clear.
# Include <stdio. h># Include <Iostream> # Include < String . H> # Include <Algorithm> Using   Namespace  STD;  Const   Int Maxn = 1010  ;  Const   Int INF = 100000000  ; Char  Str1 [maxn], str2 [maxn];  Int  A [maxn], B [maxn];  Int DP [maxn] [ 10 ] [ 10  ];  //  DP [I] [x] [Y] indicates the smallest number of operations for processing the nth Number of I. The last two digits are the smallest operands of XY.  Int  Main (){  //  Freopen ("in.txt", "r", stdin );  // Freopen ("out.txt", "W", stdout );      Int  N;  Int  X, Y;  Int  XX, YY;  Int  I, J, K;  While (Scanf ( "  % S % s  " , & Str1, & str2 )! = EOF) {n = Strlen (str1 ); For (I = 1 ; I <= N; I ++ ) {A [I] = Str1 [I- 1 ]- '  0  '  ; B [I] = Str2 [I- 1 ]- '  0  '  ;}  For (I = 0 ; I <= N; I ++ )  For (X = 0 ; X < 10 ; X ++ )  For (Y = 0 ; Y < 10 ; Y ++ ) DP [I] [x] [Y] = INF;  //  Initialization DP [ 0 ] [ 0 ] [ 0 ] = 0  ;  For (I = 1 ; I <= N; I ++) //  1 to n for DP                For (X = 0 ; X < 10 ; X ++) //  This is the number of I-1  {  If (I <=1 & Amp; X & gt; 0 ) Break  ;  For (Y = 0 ; Y < 10 ; Y ++) //  This is the number of I  {  //  Increase and decrease Transformations  //  Adjust the I-th number correctly and the number of required steps                       Int D1 = (B [I]-y + 10 ) % 10 ; //  This is forward                        Int D2 = (Y-B [I] + 10 ) % 10 ; //  This is reverse Conversion  //  The following DP process represents the state of I as the state of the I-1                        If (I = 1 ) {Xx = 0  ; YY = 0  ; DP [I] [x] [Y] = Min (DP [I] [x] [Y], DP [I- 1 ] [XX] [YY] + Min (D1, D2 ));  Continue  ;}  If (I = 2  ) {Xx = 0 ; //  The last 2nd digits of the I-1 must be 0  //  Below is the reciprocal of enumerative I-1                            For (J = x; j <= x + D1; j ++ ) {YY = J % 10  ; DP [I] [x] [Y] = Min (DP [I] [x] [Y], DP [I- 1 ] [XX] [YY] + D1 );}  For (J = x; j >=x-d2; j -- ) {YY = (J + 10 ) % 10  ; DP [I] [x] [Y] = Min (DP [I] [x] [Y], DP [I- 1 ] [XX] [YY] + D2 );}  Continue  ;}  //  Enumerating the last 1st and last 2nd digits of a I-1                        For (J = 0 ; J <= D1; j ++ )  For (K = J; k <= D1; k ++ ) {Xx = (A [I- 2 ] + J) % 10  ; YY = (X + k) % 10  ; DP [I] [x] [Y] = Min (DP [I] [x] [Y], DP [I- 1 ] [XX] [YY] + D1 );}  For (J =0 ; J <= d2; j ++ )  For (K = J; k <= d2; k ++ ) {Xx = (A [I- 2 ]-J + 10 ) % 10  ; YY = (X-K + 10 ) % 10  ; DP [I] [x] [Y] = Min (DP [I] [x] [Y], DP [I- 1 ] [XX] [YY] +D2 );}}}  If (N> = 2 ) X = A [n- 1  ];  Else X = 0  ;  If (N> = 1 ) Y = A [n];  Else Y = 0  ; Printf ( "  % D \ n  "  , DP [N] [x] [Y]);}  Return   0  ;} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.