The main topic: there is a fat man playing the dance machine, the beginning of the position in (0,0), the dance machine has four direction keys, on the left lower right respectively corresponding to 1,2,3,4. Now there are the following rules
1. If moving from 0 position to any four position, energy consumption 2
2. If you jump from a non-0 position to an adjacent position, such as 1 jumps to 2 or 4, consumes energy 3
3. If jumping from non-0 position to opposite position, such as 2 jump to 4, energy consumption 4
4. If you jump to the same position, energy consumption 1
5. Two feet cannot be in the same position
Problem-solving ideas: This problem is actually very water, direct violence can be solved, discuss all cases, with dp[i][j][k] means jump K number, left foot in I this position, right foot in J This position consumes energy, followed by classification discussion
1. If one of the feet is in the case of 0
2. The number of feet on one foot is the same as the number you are currently jumping
3. The number of two feet is different from the current number.
Three cases, respectively, in the subdivision can be, the specific look at the code
#include <cstdio>#include <algorithm>#include <cstring>using namespace Std;#define MAXN 50010#define INF 0x3f3f3f3fintdp[5][5][MAXN];intSeQ[MAXN];intstrength[2] = {4,3};intNintSolve () {memset (DP,0x3f, sizeof (DP)); dp[0][seQ[0]][0] = Dp[seQ[0]][0][0] =2; for(inti =1; I < n; i++) { for(intj =0; J <5; J + +) {if(Dp[j][seQ[i-1]][i-1] = INF) {if(J = =0) {if(SEQ[i]! = SEQ[i-1]) Dp[seQ[i]][seQ[i-1]][i] = Dp[j][seQ[i-1]][i-1] +2;if(SEQ[i]= = SEQ[i-1]) Dp[j][seQ[i-1]][i] = Dp[j][seQ[i-1]][i-1] +1;ElseDp[j][seQ[i]][i] = Dp[j][seQ[i-1]][i-1] + strength[(SEQ[i-1]+ SEQ[i]) %2]; }Else if(j = = SEQ[i]|| SeQ[i-1]= = SEQ[i]) Dp[j][seQ[i-1]][i] = min (dp[j][seQ[i-1]][i],dp[j][seQ[i-1]][i-1] +1);Else{Dp[seQ[i]][seQ[i-1]][i] = min (dp[j][seQ[i-1]][i-1] + strength[(j + SEQ[i]) %2], Dp[seQ[i]][seQ[i-1]][i]); Dp[j][seQ[i]][i] = min (dp[j][seQ[i-1]][i-1] + strength[(SEQ[i-1]+ SEQ[i]) %2], Dp[j][seQ[i]][i]); } }if(Dp[seQ[i-1]][j][i-1] = INF) {if(J = =0) {if(SEQ[i]! = SEQ[i-1]) Dp[seQ[i]][seQ[i-1]][i] = Dp[seQ[i-1]][j][i-1] +2;if(SEQ[i]= = SEQ[i-1]) Dp[seQ[i-1]][j][i] = Dp[seQ[i-1]][j][i-1] +1;ElseDp[seQ[i]][j][i] = Dp[seQ[i-1]][j][i-1] + strength[(SEQ[i-1]+ SEQ[i]) %2]; }if(j = = SEQ[i]|| SeQ[i-1]= = SEQ[i]) Dp[seQ[i-1]][j][i] = min (dp[seQ[i-1]][j][i],dp[seQ[i-1]][j][i-1] +1);Else{Dp[seQ[i]][seQ[i-1]][i] = min (dp[seQ[i-1]][j][i-1] + strength[(j + SEQ[i]) %2], Dp[seQ[i]][seQ[i-1]][i]); Dp[seQ[i]][j][i] = min (dp[seQ[i-1]][j][i-1] + strength[(SEQ[i-1]+ SEQ[i]) %2], Dp[seQ[i]][j][i]); } } } }intans = INF; for(inti =0; I <5; i++) ans = min (min (ans, dp[seQ[n-1]][i][n-1]), Dp[i][seQ[n-1]][n-1]);returnAns;}intMain () {n =0; while(SCANF ("%d", &seQ[n])! = EOF && SEq[n++]) { while(SCANF ("%d", &seQ[n]) && SEQ[n]) n++;printf("%d\ n", solve ()); n =0; }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVALive-2031 Dance Dance Revolution three-dimensional DP