Immediately after the game ended, there was a replay with the team-mates and they were beaten up.

_J is to give you a lock with 4 digits, each of which is 0-9, and then give you the current lock status and password status. You can move up or down several consecutive positions at the same time, or you can move in a single position to ask you how many last steps you need.

The common solution should be bfs, making a table and the smallest step needed to move from 0000 to any location, which I thought at first, but I don't know why my brain chose dp at the end.

Solution:

_It is easy to know that if a location is either dialed up or down, then each one is actually two cases, and the four is 222*2=16 cases.

_If an ans=need[1]+need[2]+need[3]+need[3] is dialed individually at all locations, need[i] is the number of steps required to move the first to the final from the initial to the final.

_If two consecutive digits (i,j) are simultaneously pushed up (or down), it is easy to see that the minimum number of steps required is max(need[i],need[j]).

_If three consecutive digits (i,j,k) are moved at the same time, then there are three cases. The first two are need[i] or need[k], which obviously degenerates into two consecutive digits (i,j). The third is need[j], which is the smallest. Then, it is clear that j is the first to reach the final state, then I and K will be dialed out the remaining steps separately. Then ans=need[j]+(need[i]-need[j])+(need[k]-need[j]) means ans=need[i]+need[k]-need[j].

_If four consecutive bits (i,j,k,t) are moved at the same time, then there are four cases. The first two are the minimum of need[i] or need[t], which obviously degenerates to the situation of three consecutive bits (i,j,k). The other two are the minimum of need[j] or need[k]. It is easy to get ans=need[i]+max(need[k],need[t])-need[j] and ans=need[t]+max(need[i], need[j]-need], need[k], need[k] It is the combination of three consecutive dials and two consecutive dials.

_In addition to the above, consider the case that when a need[x]==0, it can be moved along with the others at this time, and then finally dialed back to the original position, that is, there must be ans<=10 at this time.

#include <iostream> #include<bits/stdc++.h> #define scd(x) scanf("%d",&x) #define scdd(x,y) scanf("%d%d",&x,&y) #define scddd(x,y,z) scanf("%d%d%d",&x,&y,&z) #define mst(x,y) memset(x,y,sizeof(x)); using namespace std; typedef long long ll; const int maxn=1e2+5; int a[10],b[10]; int sum(int p[5],int len) { int mn=100,mx=0; if(len==4) { for(int i=1;i<=4;i++)mn=min(mn,p[i]),mx=max(mx,p[i]); if(p[2]==mn)return p[1]+max(p[3],p[4])-p[2]; if(p[3]==mn)return max(p[1],p[2])+p[4]-p[3]; if(p[1]==mn)return sum(p+1,3); else return sum(p,3); } else { for(int i=1;i<=3;i++)mn=min(mn,p[i]),mx=max(mx,p[i]); if(mn==p[2])return p[1]+p[3]-p[2]; return mx; } } int g(int k,int flag) { if(flag==0) { return a[k]<=b[k]?b[k]-a[k]:b[k]+10-a[k]; } return a[k]>=b[k]?a[k]-b[k]:a[k]+10-b[k]; } int main() { #ifdef local freopen("1.txt","r",stdin); #endif // local int T; scd(T); while(T--) { int ans=100; char s[maxn]; scanf("%s",s+1); for(int i=1;i<=4;i++)a[i]=s[i]-'0'; scanf("%s",s+1); for(int i=1;i<=4;i++)b[i]=s[i]-'0'; for(int i=0;i<=(1<<4)-1;i++) { int t=i; int p[5]; int x[5]; for(int j=1;j<=4;j++)x[j]=(t>>(j-1))&1; for(int j=1;j<=4;j++)p[j]=g(j,x[j]); for(int j=1;j<=4;j++) if(p[j]==0) ans=min(ans,10); if(x[2]==x[3])//2==3 { if(x[1]==x[2])//1==2==3 { if(x[4]==x[2])//1==2==3==4 ans=min(ans,sum(p,4)); else//1==2==3!=4 ans=min(ans,sum(p,3)+p[4]); } else if(x[4]==x[2])//1!=2==3==4 ans=min(ans,sum(p+1,3)+p[1]); else//0!=1==2!=3 ans=min(p[1]+max(p[2],p[3])+p[4],ans); } else if(x[1]==x[2]&&x[3]==x[4])ans=min(ans,max(p[1],p[2])+max(p[3],p[4])); else if(x[1]==x[2]&&x[3]!=x[4])ans=min(ans,max(p[1],p[2])+p[3]+p[4]); else if(x[1]!=x[2]&&x[3]==x[4])ans=min(ans,p[1]+p[2]+max(p[3],p[4])); else ans=min(ans,p[1]+p[2]+p[3]+p[4]); } printf("%d\n",ans); } return 0; }

We can only say that bfs is much simpler. I don't know why we have to consider so many situations to make DPS torture us.