首页 > 其他分享 >HDU 4433 locker

HDU 4433 locker

时间:2022-11-09 19:01:20浏览次数:45  
标签:10 HDU password int kk locker maxn 4433 now


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


 



f[now][i][j]表示从第1位到第now位全部已经对上且现在第now+1位是i,第now+2位是j的最少步数,然后递推转移即可

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1005;
char s1[maxn],s2[maxn];
int f[maxn][10][10],n,a[maxn],b[maxn],ans;

void get(int a,int b,int c,int x)
{
if (f[a][b][c]<0) f[a][b][c]=x;
else f[a][b][c]=min(f[a][b][c],x);
}

int main()
{
while (~scanf("%s%s",s1,s2))
{
n=strlen(s1); ans=9*n;
for (int i=1;i<=n;i++) a[i]=s1[i-1]-'0',b[i]=s2[i-1]-'0';
a[n+1]=a[n+2]=b[n+1]=b[n+2]=0;

for (int i=0;i<=n;i++) memset(f[i],-1,sizeof(f[i]));
f[0][a[1]][a[2]]=0;
for (int now=0;now<n;now++)
{
int end=a[now+3];
for (int i=0;i<10;i++)
{
int up=(b[now+1]-i+10)%10;
int dw=(i-b[now+1]+10)%10;
for (int j=0;j<10;j++)
if (f[now][i][j]>=0)
{
for (int k=0;k<=up;k++)
for (int kk=0;kk<=k;kk++)
get(now+1,(j+k)%10,(end+kk)%10,f[now][i][j]+up);
for (int k=0;k<=dw;k++)
for (int kk=0;kk<=k;kk++)
get(now+1,(10+j-k)%10,(10+end-kk)%10,f[now][i][j]+dw);
}
}
}
for (int i=0;i<10;i++)
for (int j=0;j<10;j++)
if (f[n][i][j]>=0) ans=min(f[n][i][j],ans);
printf("%d\n",ans);
}
return 0;
}



标签:10,HDU,password,int,kk,locker,maxn,4433,now
From: https://blog.51cto.com/u_15870896/5838303

相关文章

  • HDU 5586 Sum
    ProblemDescriptionThereisanumbersequence ,youcanselectainterval[l,r]ornot,allthenumbers  willbecome ..Afterthat,thesumofnnumbers......
  • HDU 4436 str2int
    ProblemDescriptionInthisproblem,youaregivenseveralstringsthatcontainonlydigitsfrom'0'to'9',inclusive.Anexampleisshownbelow.1......
  • HDU 5264 pog loves szh I
    ProblemDescriptionPoghaslotsofstrings.Andhealwaysmixestwoequal-lengthstrings.Forexample,therearetwostrings:"abcd"and"efgh".Afterm......
  • HDU 5639 Deletion
    ProblemDescriptionG with n verticesand m edges.Everytime,youcanselectseveraledgesanddeletethem.Theedgesselectedmustmeetthe......
  • HDU 5637 Transform
    ProblemDescriptionn integersaregiven.Foraninteger x youcandothefollowingoperations:+letthebinaryrepresentationof x be ......
  • HDU 1403 Longest Common Substring
    ProblemDescriptionGiventwostrings,youhavetotellthelengthoftheLongestCommonSubstringofthem.Forexample:str1=bananastr2=ciana......
  • HDU 4658 Integer Partition
    ProblemDescriptionGivenn,k,calculatethenumberofdifferent(unordered)partitionsofnsuchthatnopartisrepeatedkormoretimes.  ......
  • HDU 5638 Toposort
    ProblemDescriptionn verticesand m edges.Youareallowedtodeleteexact k InputT indicatingthenumberoftestcases.Fore......
  • HDU 4651 Partition
    ProblemDescriptionHowmanywayscanthenumbers1to15beaddedtogethertomake15?Thetechnicaltermforwhatyouareaskingisthe"numberofpart......
  • HDU 1028
    如果只有\(1\)数字,多项式为:\(1+x+x^2+x^3+\ldots\)。如果只有\(2\)数字,多项式为:\(1+x^2+x^4+x^6+\ldots\)。……如果只有\(k\)数字,多项式为:\(1+x^k+x^{2k}+x^{3......