UVA 1291 十四 Dance Dance Revolution

Dance Dance Revolution

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status Practice UVA 1291

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:

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 point 1  2  2  4. His feet may stays on (point 0, point 0)  (0, 1)  (2, 1)  (2, 1)  (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

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 const int inf=0x3f3f3f3f;
 6
 7 int a[10005];
 8 int dp[10005][5][5];
 9 int main()
10 {
11     int n;
12     int i,j,k;
13     while(scanf("%d",&a[0])!=EOF && a[0]!=0)
14     {
15         n=0;
16         while(a[n]!=0)
17         {
18             n++;
19             scanf("%d",&a[n]);
20         }
21         memset(dp,inf,sizeof(dp));
22         dp[0][a[0]][0]=2,dp[0][0][a[0]]=2;
23         for(i=1;i<n;i++)
24         {
25             for(j=0;j<=4;j++)
26             {
27                 for(k=0;k<=4;k++)
28                 {
29                     if(dp[i-1][j][k]<inf)
30                     {
31                         int x,y;
32                         if(a[i]!=k)
33                         {
34                             x=abs(a[i]-j);
35                             if(x==0)
36                                 y=1;
37                             else if(x==1 || x==3)
38                                 y=3;
39                             else if(x==2)
40                                 y=4;
41                             if(j==0)
42                                 y=2;
43                             dp[i][a[i]][k]=min(dp[i][a[i]][k],dp[i-1][j][k]+y);
44                         }
45                         if(a[i]!=j)
46                         {
47                             x=abs(a[i]-k);
48                             if(x==0)
49                                 y=1;
50                             else if(x==1 || x==3)
51                                 y=3;
52                             else if(x==2)
53                                 y=4;
54                             if(k==0)
55                                 y=2;
56                             dp[i][j][a[i]]=min(dp[i][j][a[i]],dp[i-1][j][k]+y);
57                         }
58                     }
59                 }
60             }
61         }
62
63         int ans=inf;
64         for(i=0;i<=4;i++)
65         {
66             if(dp[n-1][i][a[n-1]]<ans)
67                 ans=dp[n-1][i][a[n-1]];
68             if(dp[n-1][a[n-1]][i]<ans)
69                 ans=dp[n-1][i][a[n-1]];
70         }
71         printf("%d\n",ans);
72     }
73     return 0;
74 }

时间: 2024-10-01 13:18:22

UVA 1291 十四 Dance Dance Revolution的相关文章

递推DP UVA 1291 Dance Dance Revolution

题目传送门 1 /* 2 题意:给一串跳舞的动作,至少一只脚落到指定的位置,不同的走法有不同的体力消耗,问最小体力消费多少 3 DP:dp[i][j][k] 表示前i个动作,当前状态(j, k)的最小消费,状态转移方程:(a[i], k) <- min (a[i-1], k) + cost 4 以及(a[i-1], a[i]) <- min (a[i-1], k) + cost, (k, a[i])和(a[i], a[i-1])情况类似,最后再去最小值就行了 5 */ 6 /*********

uva 1291 dp

UVA 1291 - Dance Dance Revolution 有一个跳舞机.原点为0,有四个方向,上左下右,分别标成(1234),初始玩家两只脚站在 0 位置,跳舞机会给出一串数字,玩家要按照顺序踩下四个方向的数字.移动脚会消耗玩家的能量,从0位置移动到四个方向消耗2点能量,从一个方向移动到另一个相邻的方向消耗3点能量,从一个方向移动到相反方向消耗4点能量,原点踩一下消耗1点能量.问你踩出这串数子最少要花多少能量. 根据能量消耗关系,我们可以发现当前两只脚踩的方向才是重点.然而要记录两只脚

Java进阶(三十四)Integer与int的种种比较你知道多少?

Java进阶(三十四)Integer与int的种种比较你知道多少? 前言 如果面试官问Integer与int的区别:估计大多数人只会说到两点:Ingeter是int的包装类,注意是一个类:int的初值为0,Ingeter的初值为null.但是如果面试官再问一下Integer i = 1;int ii = 1; i==ii为true还是为false?估计就有一部分人答不出来了,如果再问一下其他的,估计更多的人会头脑一片混乱.所以我对它们进行了总结,希望对大家有帮助. 首先看代码: package

centos LNMP第二部分nginx、php配置 第二十四节课

centos  LNMP第二部分nginx.php配置  第二十四节课 上半节课 下半节课 f

从零开始学android&lt;android事件的处理方式.二十四.&gt;

在android中一共有 多种事件,每种事件都有自己相对应的处理机制 如以下几种 1 单击事件 View.OnClickListener public abstract void onClick (View v) 单击组件时触发 2 单击事件 View.OnLongClickListener public abstract boolean onLongClick (View v) 长按组件时触发 3 键盘事件 View.OnKeyListener public abstract boolean

苹果新的编程语言 Swift 语言进阶(十四)--扩展

Swift语言的扩展是为一个已经存在的类.结构.枚举类型添加新功能的一种方式,包括为不能存取源代码的那些已经存在的类型添加功能.        扩展类似于Objective-C语言中的类别,与类别不同的是Swift语言的扩展没有名字.        扩展能够为已存在类型:     1)增加计算属性和计算静态属性:     2)定义新的实例方法和类型方法:     3)提... 请转移到此位置继续阅读,谢谢! 苹果新的编程语言 Swift 语言进阶(十四)--扩展 苹果新的编程语言 Swift 语

第三百六十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mapping映射管理

第三百六十四节,Python分布式爬虫打造搜索引擎Scrapy精讲-elasticsearch(搜索引擎)的mapping映射管理 1.映射(mapping)介绍 映射:创建索引的时候,可以预先定义字段的类型以及相关属性elasticsearch会根据json源数据的基础类型猜测你想要的字段映射,将输入的数据转换成可搜索的索引项,mapping就是我们自己定义的字段数据类型,同时告诉elasticsearch如何索引数据以及是否可以被搜索 作用:会让索引建立的更加细致和完善 类型:静态映射和动态

第十四篇 现象

第十四篇  现象 "现象"的产生是由宇宙当中各种因素交汇的结果.现象是万物在宇宙中的展现,它可以被人为创造,也可以由宇宙规律自行产生.现象能帮助人类逐步地了解宇宙的本质,也能帮助人类更好地了解自己. 当一个生命对宇宙有足够高度的认识之后就会留意所有的现象,并从这些现象当中去探索自身以及宇宙的奥秘.可以说,生命的成长过程就是不断地分析与探索各种现象,从而总结经验,让自身不断提高探索宇宙奥秘能力的一个历练过程. 随着对各种现象的不断分析与探索,人类会越来越深刻地认识到现象背后的本质,而不会

code第一部分:数组 第十四题 雨水问题

code第一部分:数组 第十四题 雨水问题 Given n non-negative integers representing an elevation map where the width of each bar is 1, computehow much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. 分析: 解决方案1 对于每个柱子,找到其左右两