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 ;
}

最新文章

  1. 关于C++中的cout
  2. 中间人攻击(MITM)姿势总结
  3. 采用ubuntu系统来安装tensorflow
  4. Codeforce - Runtime Error
  5. mybatis配置oracle的主键自增长
  6. sell - 配置service
  7. UVA 568 Just the Facts (水)
  8. chmod 命令
  9. maven 依赖冲突的问题
  10. URL 调度器(URL dispatcher)
  11. js实用方法记录-js动态加载css、js脚本文件
  12. mysql时间戳与日期格式的相互转换
  13. 1.11 str 字符串
  14. Triangle (第8届山东省赛的某题)
  15. eclipse如何修改android工程的包名?
  16. C#退出窗体的总结方法
  17. boost::function 介绍
  18. 利用mimikatz破解远程终端凭据,获取服务器密码
  19. 将GETDATE()转换为指定日期格式的varchar类型
  20. Flume的Source

热门文章

  1. 成为IT精英,我奋斗了7年
  2. 另一种root方法,Android boot.img破解
  3. ubuntu &quot;mkdir -p&quot;命令
  4. 【转】const的用法,特别是用在函数前面与后面的区别!
  5. 利用GPS获取行车速度和距离
  6. LeetCode_Combination Sum
  7. 利用dedecms给近三天(或当天)发布的文章显示红色日期或加上new字或new小图片
  8. angularjs基本执行流程
  9. openssl 证书请求和自签名命令req详解
  10. ios浅谈关于nil和 NIL区别及相关问题(转)