Exercises
Simple DP
Dp[i][j][k] represents the position of step I feet in position J and position K
And then you can push the transfer equation according to test instructions.
Code:
#include <bits/stdc++.h>using namespacestd;#definePB Push_back#defineMP Make_pair#defineSe Second#defineFS First#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#definePII pair<int,int>#definell Long LongConst intINF =1e9;Const intMAXN =50050; Vector<int>v;intx;intdp[MAXN [5][5 ];intStep_num (intSinte) { if(s = =0)return 2; if(s = =1 ) { if(E = =1)return 1; if(E = =2|| E = =4)return 3; if(E = =3)return 4; } if(s = =2 ) { if(E = =2)return 1; if(E = =1|| E = =3)return 3; if(E = =4)return 4; } if(s = =3 ) { if(E = =3)return 1; if(E = =2|| E = =4)return 3; if(E = =1)return 4; } if(s = =4 ) { if(E = =4)return 1; if(E = =1|| E = =3)return 3; if(E = =2)return 4; }}intMain () { while(~SCANF ("%d", &x) &&x) {v.clear (); V.push_back (x); while(SCANF ("%d", &x) &&x) v.push_back (x); for(inti =0; I <5; i + + ) for(intj =0; J <5; J + + ) for(intK =0; K <50000; K + +) dp[k [i] [j] =INF; intn = v.size (), Goal =INF; dp[1][0[v[0]] = dp[1[v[0] ][0] =2; for(inti =2; I <= N; i + + ) { intnow = v[I-1 ]; for(intj =0; J <5; J + + ) for(intK =0; K <5; K + + ) { if(j = k)Continue; if(J! = Now) dp[I [j] [Now] = min (dp[I [j] [now], dp[i-1[j] [K] +Step_num (k, now)); if(k! = Now) dp[I [now] [k] = min (dp[i] [now] [K], dp[i-1[j] [K] +Step_num (J, now)); if(i = =N) {goal=min (goal, dp[I [now] [K]); Goal=min (goal, dp[I [j] [now]); }}} printf ("%d\n", goal); } return 0;}
Uvalive 2031Dance Dance Revolution