locker(dp) HDU - 4433

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?

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 Input

111111 222222
896521 183995

Sample Output

2
12
Meaning of the questions: to two equal length strings with a maximum of three figures can be rotated continuously, attention can be rotated 0 to 9, and asked how many times to a minimum of changes to the S series T string
Analysis: see title naturally can think of dp, we can assume that dp (i, j, k) indicates the position of the i-th position and before and has the same purpose digital, i + 1-th position j, the i position k + 2
For example: We assume that dp (i-1, j, k) denotes the front i-1 positions (including the first i-1 positions) have been and purposes the same figure, the i-th position j, i + 1-th position k, j we enumerate from 0 ~ 9, k, where there are two cases
: Rising or falling Enumeration enum Further, note that in the process of the state transition in the second position is less than the number of conversions to convert a first position.
The state transition equation is:
升序枚举:dp[i][(k-p+10)%10][(a[i+2]-q+10)%10]=min(dp[i][(k-p+10)%10][(a[i+2]-q+10)%10],dp[i-1][j][k]+up);
降序枚举:dp[i][(k+p)%10][(a[i+2]+q)%10]=min(dp[i][(k+p)%10][(a[i+2]+q)%10],dp[i-1][j][k]+down);
 
AC Code:
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn = 1e3+10;
 7 #define LL long long
 8 #define INF 0x3f3f3f3f
 9 char s1[maxn],s2[maxn];
10 int a[maxn],b[maxn];
11 int dp[maxn][10][10];
12 
13 
14 int main(){
15     int i,j,k,p,q,n,m,len,up,down;
16     while(~scanf("%s%s",s1+1,s2+1)){
17         len=strlen(s1+1);
18         a[0]=b[0]=0;
19         a[len+1]=a[len+2]=0;
20         b[len+1]=b[len+2]=0;
21         for(int i=1;i<=len;i++){
22             a[i]=s1[i]-'0';
23             b[i]=s2[i]-'0';
24         }
25         memset(dp,INF,sizeof(dp));
26         dp[0][a[1]][a[2]]=0;
27         for(int i=1;i<=len;i++){
28             for(int j=0;j<10;j++){
29                 for(int k=0;k<10;k++){
30                     if(dp[i-1][j][k]==INF) continue;
31                     down=(b[i]-j+10)%10;
32                     for(p=0;p<=down;p++) for(q=0;q<=p;q++){
33                         dp[i][(k+p)%10][(a[i+2]+q)%10]=min(dp[i][(k+p)%10][(a[i+2]+q)%10],dp[i-1][j][k]+down);
34                     }
35                     up=10-down;
36                     for(p=0;p<=up;p++) for(q=0;q<=p;q++){
37                         dp[i][(k-p+10)%10][(a[i+2]-q+10)%10]=min(dp[i][(k-p+10)%10][(a[i+2]-q+10)%10],dp[i-1][j][k]+up);
38                     }
39                 }
40             }
41         }
42         printf("%d\n",dp[len][0][0]);
43     }
44 
45 
46 
47     return 0;
48 }
49 /*
50 111111 222222
51 896521 183995
52 */

 

 

Guess you like

Origin www.cnblogs.com/Bravewtz/p/11364055.html