Title: There are 4 positions, each moving from one position to another requires a specific cost, the minimum cost of a sequence of specified sequences. Only one foot at a time can move, or move and step down.
Use D[i][j][u] to indicate that the first I position is complete and the minimum cost in the state (J,u), (J,u) One of which must be equal to A[i], assuming J and A[i] are equal, then either the previous state is (j,u), or the previous state is (k,u), note that the previous state cannot be (j,k), because this requires a left foot pedal, right foot movement, illegal. As for whether the mobile is left or right foot, do not care, because can always be transferred.
State transition equation:
D[i][j][u]=min {d[i-1][j][u]+1,min {d[i-1][k][u]+cost[j][k]}} (J==a[i])
D[i][j][u]=min {d[i-1][j][u]+1,min {d[i-1][j][k]+cost[k][u]}} (U==a[i])
#include <stdio.h> #include <stdlib.h>int d[2][7][7];int cost[5][5]={{1,2,2,2,2},{2,1,3,4,3},{2,3,1,3,4 },{2,4,3,1,3},{2,3,4,3,1}};int Main (void) {int i,j,u,cur,min,minp;scanf ("%d", &u), while (u!=0) {for (i=0;i<5;i + +) {for (j=0;j<5;j++) {d[0][i][j]= (1<<29);}} CUR=0;D[0][0][U]=D[0][U][0]=2;SCANF ("%d", &u), while (u!=0) {cur^=1;for (i=0;i<5;i++) {for (j=0;j<5;j++) {d[ Cur][i][j]= (1<<29);}} for (i=0;i<5;i++) {if (i!=u) {minp=d[cur^1][i][u]+1;for (j=0;j<5;j++) {if (j!=i) {minp= (d[cur^1][i][j]+cost[j][ U]<MINP)? (D[cur^1][i][j]+cost[j][u]): MINP);}} D[CUR][I][U]=D[CUR][U][I]=MINP;}} D[cur][u][u]= (1<<29); scanf ("%d", &u);} Min= (1<<29); for (i=0;i<5;i++) {for (j=0;j<5;j++) {if (d[cur][i][j]<min) {min=d[cur][i][j];}}} printf ("%d\n", Min); scanf ("%d", &u);} return 0;}
UVA 1291-dance Dance Revolution (DP)