UVA1631 - Locker

来源:互联网 发布:python agv[] 编辑:程序博客网 时间:2024/05/22 09:44

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

我有话说:
这道题的动态规划思路不太好想,我是在看了网上一篇博文后才写出来的。
我们设对于一次操作,我们取其最后一位数作为操作数。如000到111去视第三位数位操作数。由此可以预处理出cost[i][j]数组。代表三位数i变成j的最小次数。
然后处理密码锁字符串。我们设dp[i][j] 为前i-2个数字已经处理到位,第i-1个数和第i个数组成二位数j的状态下的最小次数。那么它可以由dp[i-1][u]状态转化而得。其中设p=u*10+s1[i],q=s2[i-2]*100+j.那么我们就可以利用预处理出的cost[p][q]得到动态规划方程,即为
dp[i][j]=min{dp[i-1][u]+cost[p][q]},p=u*10+s1[i] ,q=s2[i-2]*100+j.

#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <queue>#include <cstring>using namespace std;const int maxn=1000+10;const int INF=100000000;char s1[maxn],s2[maxn];int cost[maxn][maxn],dp[2][maxn];//cost[i][j]表示三位数i变成三位数j所需的最小转动次数。void Init(){    int i1,i2,i3,j1,j2,j3,u1,u2,u3;    for(int i=0;i<1000;i++){        for(int j=0;j<1000;j++){            if(i!=j){                if(cost[j][i]){cost[i][j]=cost[j][i];continue;}                i1=i/100;i2=(i/10)%10;i3=i%10;                j1=j/100;j2=(j/10)%10;j3=j%10;                if(i1<j1){                    u1=j1-i1;                    i2=(i2+u1)%10;                    i3=(i3+u1)%10;                }else{                    u1=i1-j1;                    i2=(i2-u1+10)%10;                    i3=(i3-u1+10)%10;                }                if(i2<j2){                    u2=j2-i2;                    i3=(i3+u2)%10;                }else{                    u2=i2-j2;                    i3=(i3-u2+10)%10;                }                u3=(i3>j3)?i3-j3:j3-i3;                u1=(u1>10-u1)?10-u1:u1;                u2=(u2>10-u2)?10-u2:u2;                u3=(u3>10-u3)?10-u3:u3;                cost[i][j]=u1+u2+u3;            }        }    }}int main(){    Init();    while(scanf("%s%s",s1+1,s2+1)==2){        int len=strlen(s1+1);        if(len==1){            int t=s1[1]>s2[1]?s1[1]-s2[1]:s2[1]-s1[1];            printf("%d\n",t<10-t?t:10-t);            continue;        }        int p,q,ans;        for(int i=0;i<10;i++){            int t=i>s1[1]-'0'?i-s1[1]+'0':s1[1]-'0'-i;            dp[0][i]=min(t,10-t);        }        for(int i=0;i<100;i++){            ans=INF;            for(int u=0;u<10;u++){                q=u*10+s1[2]-'0';                ans=min(dp[0][u]+cost[q][i],ans);            }            dp[1][i]=ans;        }        //边界预处理        int t=1;        for(int i=3;i<=len;i++){            t^=1;            for(int j=0;j<100;j++){                ans=INF;                for(int u=0;u<100;u++){                    p=u*10+s1[i]-'0';                    q=(s2[i-2]-'0')*100+j;                    ans=min(ans,dp[t^1][u]+cost[p][q]);                }                dp[t][j]=ans;            }        }        q=(s2[len-1]-'0')*10+s2[len]-'0';        printf("%d\n",dp[t][q]);    }    return 0;}
0 0
原创粉丝点击