【动态规划】locker

locker

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1271    Accepted Submission(s): 549


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
 

Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:   4812  4811  4810  4809  4808 
 
 
每次只能扳动一格。
暂讨论向上扳。
 
因为一次最多可以转动三格子。转移方程中涉及到了i,i+1,i+2的状态
f[i,j,k] 表示第i格已经扳到j,i+1格已经扳到k。前i-1个格子都到指定位置。的最小转动次数。
要把i格扳到指定位置,需要扳(s2[i]-s1[i]-j)%10次。
然而并不是每次都得转动i,i+1,i+2。也可以只转动i,i+1或只转动i。(我们可以想象成每次都是i主动转,带动后两个可转可不转)因此t>=a>=b。
(注意k和a的含义不同,j和t的含义也不同。因为j和k是之前被动地转,a是当前i+1被动地转,t是i主动在转)
 
向下转类似。
最后输出答案是最后一个格子后面两个格子都不转。
 
 
这道题的启示是:
1、无序有序化,有时就可以消除后效性(当然要利用一些手段比如整体看待)。
2、有多少元素参与转移,常常就会有多少元素在转移方程里,把它们当做一个整体来看待。
 
 
#include 
#include 

int min(int a,int b)
{
    return a

你可能感兴趣的:(ACM)