UVa 1291- Dance Dance Revolution(DP)

来源:互联网 发布:java空指针异常例子 编辑:程序博客网 时间:2024/04/29 12:28

Dance Dance Revolution

Time Limit:3000MS   

Memory Limit:0KB   

64bit IO Format:%lld & %llu

Submit Status

Description

Download as PDF

Mr. White, a fat man, now is crazy about a game named ``Dance, Dance, Revolution". But his dance skill is so poor that 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 way. He still expects that he will be regarded as ``Terpsichorean White" one day. So he is considering writing a program to plan the movement sequence of his feet, so that he may save his strength on dancing. Now he looks forward to dancing easily instead of sweatily.

``DDR" is a dancing game that requires the dancer to use his feet to tread on the points according to the direction sequence in the game. There are one central point and four side points in the game. Those side points are classified as top, left, bottom and right. For the sake of explanation, we mark them integers. That is, the central point is 0, the top is 1, the left is 2, the bottom is 3, and the right is 4, as the figure below shows:

\epsfbox{p2031.eps}

At the beginning the dancer's two feet stay on the central point. According to the direction sequence, the dancer has to move one of his 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 to point 1 (Note: Not both of his 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 that stays on point 0 or the one that stays on point 1.

There is a strange rule in the game: moving both of his feet to the same point is not allowed. For instance, if the sequence requires the dancer to the bottom point and one of his feet already sta ys on point 3, he should 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 will be consumed when he moves from one point to another. Moving one of his feet from the central point to any side points will consume 2 units of his strength. Moving from one side point to another adjacent side point will consume 3 units, such as from the top point to the left point. Moving from one side point to the opposite side point will consume 4 units, such as from the top point to the bottom point. Yet, if he stays on the same point and tread again, he will use 1 unit.

Assume that the sequence requires Mr. White to move to point1 $ \rightarrow$ 2$ \rightarrow$ 2$ \rightarrow$ 4. His feet may stays on (point 0, point 0)$ \rightarrow$ (0, 1)$ \rightarrow$ (2, 1)$ \rightarrow$ (2, 1)$ \rightarrow$ (2, 4). In this couple of integers, the former number represents the point of his left foot, and the latter represents the point of his right foot. In this way, he has to consume 8 units of his strength. If he tries another pas, he will have to consume much more strength. The 8 units of strength is the least cost.

Input

The input file will consist of a series of direction sequences. Each direction sequence contains a sequence of numbers. Ea ch number should either be 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 be 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 will be consumed. The result should be a single integer on a line by itself. Any more white spaces or blank lines are 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


题意:

怀特在跳舞,初始时间左右脚都在 0 位置。给出的一系列数字由 1, 2, 3, 4组成(不包含0).读取到 0 表示该例子结束,0 不做处理。

每次你必须选择一只脚移动到对应数字方向的各格子上。(x, y)表示左右脚分别在什么位置。

初始状态(0,0), 要移到 1, 可以选择左脚或者右脚移上去,对应的状态为(1, 0), (0,1)

移到相临边的格子需要 2 单元的体力,移到对角线的格子需要 3 体力。若原地不动,1 体力。移到前方的第二个格子,需要2 体力。除了初始位置,左右脚都在同一格子(都在0号格子),其他状态都不允许左右脚在同一格子。


思路:用DP求解。

状态表示: dp [ i ] [ j ] [ k ] 表示执行完第 i 个指令后,左脚在 j 位置,右脚在 k 位置时消耗的最少体力

状态转移方程:

1:左脚不动,右脚从 k 位置转移到 a[i],即dp[i][j][a[i]] 由 dp[i-1][j][k] 转化而来

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

1:右脚不动,左脚从 k 位置转移到 a[i],即dp[i][a[i]][j] 由dp[i-1][k][j] 转化而来
dp[i][a[i]][j] = min( dp[i-1][k][j]+cost, dp[i][a[i]][j] ),

其中:cost = f(a[i], k),   a[i] 和 k之间转化的消耗


<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; // 第一步左脚移动        dp[1][0][s] = 2; // 第一步右脚移动        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 和 k 均从 0 开始,不是从 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>


0 0