Dance Dance Revolution

Time limit: 3.000 seconds

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
Shanghai 2000-2001

题解:

线性动态规划。维护一个三维数组f[i][x][y]表示在第i步左脚在x,右脚在y消耗最小的体力。

注意初始化f=MAX_NUMBER。f[1][a[1]][0]=2;f[1][0][a[1]]=2;

动态转移方程:

f[i][a[i]][j]=min(f[i][a[i]][j],f[i-1][k][j]+effort(k,a[i]));

f[i][j][a[i]]=min(f[i][j][a[i]],f[i-1][j][k]+effort(k,a[i]));

代码:

 #include<stdio.h>
#include<string.h>
#include<stdbool.h>
int i,j,n,
a[],f[][][]; int
pre()
{
memset(a,,sizeof(a));
memset(f,,sizeof(f));
return ;
} int
init()
{
while(true)
{
n++;
scanf("%d",&a[n]);
if(a[n]==)
{
n--;
break;
}
} f[][a[]][]=;
f[][][a[]]=;
return ;
} int
max(int a,int b)
{
if(a>b) return(a);
else return(b);
} int
min(int a,int b)
{
if(a<b) return(a);
else return(b);
}
int
effort(int a,int b)
{
int p;
if((min(a,b)==)&&(max(a,b)!=)) return();
p=max(a,b)-min(a,b);
if(p== || p==) return();
if(p==) return();
if(p==) return();
} int
dp()
{
int i,j,k,mini;
for(i=;i<=n;i++)
for(j=;j<=;j++)
for(k=;k<=;k++)
{
f[i][a[i]][j]=min(f[i][a[i]][j],f[i-][k][j]+effort(k,a[i]));
f[i][j][a[i]]=min(f[i][j][a[i]],f[i-][j][k]+effort(k,a[i]));
} mini=;
for(i=;i<=;i++)
mini=min(mini,min(f[n][a[n]][i],f[n][i][a[n]])); printf("%d\n",mini);
return ;
} int
main()
{
while(true)
{
pre();
n=;
scanf("%d",&a[n]);
if(a[n]==) break;
init();
dp();
}
return ;
}

[LA] 2031 Dance Dance Revolution的更多相关文章

  1. UVA 1291 十四 Dance Dance Revolution

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

  2. 递推DP UVA 1291 Dance Dance Revolution

    题目传送门 题意:给一串跳舞的动作,至少一只脚落到指定的位置,不同的走法有不同的体力消耗,问最小体力消费多少分析:dp[i][j][k] 表示前i个动作,当前状态(j, k)的最小消费,状态转移方程: ...

  3. Dance Dance Revolution

    今天我们来讲 Dance Dance Revolution这题 本题原网址 注意本题为多组输入输出,直到输入单个零而止(题面有点小问题) 很明显,此题为一道动态规划题(请不要妄想用贪心算法过这题,尽管 ...

  4. LA 2031

    Mr. White, a fat man, now is crazy about a game named ``Dance, Dance, Revolution". But his danc ...

  5. UVALive - 2031 Dance Dance Revolution 三维dp

    题目大意:有一个胖子在玩跳舞机.刚開始的位置在(0,0).跳舞机有四个方向键,上左下右分别相应1,2,3,4.如今有下面规则 1.假设从0位置移动到随意四个位置,消耗能量2 2.假设从非0位置跳到相邻 ...

  6. 2000 Asia shanghai Dance Dance Revolution

    思路:dp[i][x][y]表示第i个序列中,右脚在x位置,左脚在y位置时,其最小花费. 那么dp[i][x][y]=min(dp[i-1][a[i]][y]+cost[a[i]][x],dp[i-1 ...

  7. UVA 1291 Dance Dance Revolution(DP)

    意甲冠军:跳舞机有一个上5积分,分别central, top, bottom, left, right分,区区足站立还是需要1点物理,从一个单纯的脚central点上须要2点体力,从一个点上移动到相邻 ...

  8. TCP/UDP端口列表

    http://zh.wikipedia.org/wiki/TCP/UDP%E7%AB%AF%E5%8F%A3%E5%88%97%E8%A1%A8 TCP/UDP端口列表     本条目可通过翻译外语维 ...

  9. TCP/UDP 常用端口列表

    计算机之间依照互联网传输层TCP/IP协议不同的协议通信,都有不同的对应端口.所以,利用短信(datagram)的UDP,所采用的端口号码不一定和采用TCP的端口号码一样.以下为两种通信协议的端口列表 ...

随机推荐

  1. 成为一名优秀的C程序员

    英文原文:To become a good C programmer 问题的提出 每过一段时间我总会收到一些程序员发来的电子邮件,他们会问我是用什么编程语言来编写自己的游戏的,以及我是如何学习这种编程 ...

  2. 安装notepad++之后怎样在鼠标右键上加上Edit with notepad++

    在鼠标右键上加入使用notepad++编辑 我们在安装完notepad++文本编辑器之后,在一个文本文件上右键有时候并没有出现"使用notepad++编辑的选项",我们可以通过简单 ...

  3. JVM试用G1的垃圾收集器

    因为以前用默认的GC,,老年代经常在占比超过99%才发生一个GC行为,感觉不爽...尽管每次FULL GC只要0.5S. 结合上次听中华构架师大会,一哥们分享的G1 GC...试试.. 就在TOMCA ...

  4. RFID电子标签加工的倒装工艺

    倒装对于半导体封装领域的人员而言,是再熟悉不过的了.一般我们看到的集成电路多数以塑封为主,半导体芯片和外界进行信息沟通的通道,靠的就是集成电路的管脚.如果把集成电路外面的封装去掉,会发现每个集成电路内 ...

  5. 自制单片机之十……AT89S51的上拉电阻问题

    很多网友都问我AT89S51的P0口为什么要接一个上拉电阻.我就用一个篇幅来说一说 P0口和其它三个口的内部电路是不同的,如下图 P0口是接在两个三极管D0和D1之间的,而P1-P3口的上部是接一个电 ...

  6. Linux sleep命令 和 wait命令

    man sleep: SLEEP(1) User Commands SLEEP(1) NAME sleep - delay for a specified amount of time SYNOPSI ...

  7. @@ROWCOUNT 含义

    返回受上一语句影响的行数. 如果行数大于 20 亿,请使用 ROWCOUNT_BIG. Transact-SQL 语句可以通过下列方式设置 @@ROWCOUNT 的值: 将 @@ROWCOUNT 设置 ...

  8. 【转】 boot.img的解包与打包

    原文网址:http://blog.csdn.net/wh_19910525/article/details/8200372 Android 产品中,内核格式是Linux标准的zImage,根文件系统采 ...

  9. 关于bootstrap--表单(水平表单)

    在Bootstrap框架中要实现水平表单效果,必须满足以下两个条件:1.在<form>元素是使用类名“form-horizontal”.2.配合Bootstrap框架的网格系统.(网格布局 ...

  10. 基于PCA和SVM的人脸识别

    程序中采用的数据集是ORL人脸库,该人脸库共有400副人脸图像,40人,每人10幅,大小为112*92像素,同一个人的表情,姿势有少许变化. 程序的流程主要分为三部分,数据的预处理(PCA降维和规格化 ...