UVA 1291-dance Dance Revolution (DP)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.