hdu 4433 locker(DP,4级)

C - locker
Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status
Appoint description:  System Crawler  (2013-09-09)

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

思路:dp+枚举 dp[len][j][k] 前len位,不包含当前位移动到终态,影响i位移动j,i+1位移动k的最优值。

             枚举移动影响的两位,当然枚举如果是a,b,c a<=b<=c ,

              1000X10X10X10X10

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=1009;
const int oo=1e9;
char s[mm],t[mm];
int dp[mm][10][10];///前i位OK不包含第i位,i移动j,i+1移动k
int main()
{
  while(~scanf("%s%s",s,t))
  {
    int len=strlen(s);
    FOR(i,0,len)FOR(j,0,9)FOR(k,0,9)
    dp[i][j][k]=oo;
    dp[0][0][0]=0;
    FOR(i,0,len-1)
    {
      FOR(j,0,9)FOR(k,0,9)
      {
        if(dp[i][j][k]==oo)continue;
//        puts("yes");
        int zk=(t[i]-s[i]-j+30)%10;
        FOR(a,0,zk)FOR(b,0,a)///正转
        dp[i+1][(k+a)%10][b]=min(dp[i+1][(k+a)%10][b],dp[i][j][k]+zk);
        zk=10-zk;  ///逆转
        FOR(a,0,zk)FOR(b,0,a)
        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]+zk);
//        cout<<dp[i+1][0][0]<<endl;
      }
    }
    printf("%d\n",dp[len][0][0]);
  }
}




你可能感兴趣的:(hdu 4433 locker(DP,4级))