UVa 1291-dance Dance Revolution (DP)

Source: Internet
Author: User

Dance Dance Revolution

Time Limit: the Ms

Memory Limit: 0 Kb

64bit IO Format: %lld &%llu

Submit Status

Description

Mr White, a fat man, and now are crazy about a game named "Dance, Dance, Revolution". But his dance skill was so poor the He could not dance a dance, even if he dances arduously every time. Does ' DDR ' just mean him a perfect method to squander his pounds? No. He still expects that he'll be regarded as ' Terpsichorean white ' one day. So he was considering writing a program to plan the movement sequence of his feet, so that he could save his strength on Danc Ing. Now he looks forward to dancing easily instead of sweatily.

' DDR ' is a dancing game This requires the dancer to use he feet to tread on the points according to the direction Sequen Ce in the game. There is one central point and four side points in the game. Those side points is classified as top, left, bottom and right. For the sake of explanation, we mark them integers. That was, the central point was 0, the top is 1, the left was 2, the bottom is 3, and the right is 4, as the figure below sho vs[

At the beginning the dancer ' s, the feet stay on the central point. According to the direction sequence, the dancer have to move one of its feet to the special points. For example, if-the sequence requires him to move-to-the-top point at first, he may move either of his feet from point 0 t O point 1 (Note:not bothof He feet). Also, if the sequence then requires him-to-move to the bottom point, he may move either of his feet to point 3, regardless Whether to use the foot, stays on point 0 or the one, stays on point 1.

There is a strange rule in the game:moving both of he feet to the same point was not allowed. For instance, if the sequence requires the dancer to the bottom point and one of its feet already STA ys on point 3, he sh Ould stay the very foot on the same point and tread again, instead of moving the other one to point 3.

after dancing for a long time, Mr. White can calculate how much strength would be consumed When the he moves from one point to another. Moving one of his feet from the central point to any side points would consume 2 units of his strength. Moving from one side point to another adjacent side point would consume 3 units, such as from the top point to the left POI NT. Moving from one side point to the opposite side point would consume 4 units, such as from the top point to the bottom P Oint. Yet, if he stays on the same point and tread again, he'll use 1 unit.

Assume that the sequence requires Mr. White to move to point1 2 2 4. His feet could be stays on (point 0, point 0) (0, 1) (2, 1)(2, 1) (2 and 4). In this couple of integers, the former number represents the point of he left foot, and the latter represents the point O F his right foot. In the this-to-consume 8 units of he strength. If He tries another pas, he'll has to consume much more strength. The 8 units of strength is the least cost.

Input

The input file is consist of a series of direction sequences. Each direction sequence contains a sequence of numbers. Ea CH number should either is 1, 2, 3, or 4, and each represents one of the four directions. A value of 0 in the direction sequence indicates the end of direction sequence. And this value should is excluded from the direction sequence. The input file ends if the sequence contains a single 0.

Output

For each direction sequence, print the least units of strength would be consumed. The result should is a single integer on a line by itself. Any more white spaces or blank lines is not allowable.

Sample Input

1 2 2 4 0 1 2 3 4 1 2 3 3 4 2 0 0

Sample Output

8 22


Test instructions

White is dancing, the initial time left and right feet are in 0 position. The given series of numbers consists of 1, 2, 3, 4 (not including 0). Reading to 0 means that the example ends, and 0 does not do the processing.

Each time you have to choose one foot to move to the corresponding number of squares on the direction. (x, Y) indicates where the left and right feet are respectively.

Initial state (0,0), to move to 1, you can choose the left foot or the right foot to move up, corresponding to the status of (1, 0), (0,1)

moving to the opposite side of the lattice requires 2 units of physical strength, moving to the diagonal grid requires 3 stamina. If you do not move, 1 physical strength. Moving to the second grid in front requires 2 stamina. In addition to the initial position, the left and right feet are in the same lattice (both in the No. 0 lattice), other states are not allowed to the left and right foot in the same lattice.


IDEA: Solve with DP.

Status indication: DP [i] [j] [K] indicates that the left foot is at J position and the minimum amount of energy consumed at the right foot at K position after executing the i instruction

State transition equation:

1: Left foot motionless, right foot from the K position transferred to A[i], that is dp[i][j][a[i] "by Dp[i-1][j][k]

Dp[i][j][a[i]] = min (Dp[i-1][j][k]+cost, Dp[i][j][a[i]]),

1: The right foot does not move, the left foot moves from the K position to a[i], namely DP[I][A[I]][J] by Dp[i-1][k][j] transformed.
Dp[i][a[i]][j] = min (dp[i-1][k][j]+cost, dp[i][a[i]][j]),

Where: Cost = f (A[i], k), A[i] and K conversion consumption


<span style= "FONT-SIZE:18PX;" > #include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include < string> #include <algorithm> #include <queue> #include <stack>using namespace Std;const int INF = 0x3f3f3f3f;const int maxn = 1010;const Double PI = ACOs ( -1.0); const double e = 2.718281828459;const double EPS = 1e-8;int    Dp[maxn][5][5];int a[maxn];int F (int x, int y) {if (x = = y) return 1;    if (x==0 | | y==0) return 2; if ((x==3&&y==1) | | | (x==1&&y==3) | | (x==2&&y==4) | |        (x==4&&y==2))    return 4; return 3;}    int main () {//freopen ("In.txt", "R", stdin);    Freopen ("OUT.txt", "w", stdout);    int s;        while (Cin>>s && s) {int n = 0;        Memset (DP, INF, sizeof (DP)); Dp[1][s][0] = 2; First step left foot move dp[1][0][s] = 2;        First step right foot move a[++n] = s;        while (Cin>>s && s) {a[++n] = s; } for (int i = 2; I <= N;  i++) {for (int j = 0; J <= 4; j + +) {//J and K are all starting from 0, not starting from 1 for (int k = 0; K <= 4;                    k++) {int cost = f (A[i], k);                    Dp[i][j][a[i]] = min (Dp[i-1][j][k]+cost, dp[i][j][a[i]]);                Dp[i][a[i]][j] = min (dp[i-1][k][j]+cost, dp[i][a[i]][j]);        }}} int ans = INF; for (int i = 0; I <= 4; i++) {for (int j = 0; J <= 4; j + +) ans = min (ans, dp[n][i][j        ]);    } cout<<ans<<endl; } return 0;} </span>


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.