经典DP跳舞怀特

View Code
 1 var
2 f:array[0..10000,1..5,1..5] of longint;
3 a:array[1..10000] of integer;
4 n,i,j,k,p,q,b1,b2,minn:longint;
5
6 function min(a,b:longint):longint;
7 begin
8 if a>b then exit(b) else exit(a);
9 end;
10
11 function change(a,b:integer):integer;
12 begin
13 if a=b then exit(1) else
14 if a=5 then exit(2) else
15 if abs(a-b)=2 then
16 exit(4) else exit(3);
17 end;
18
19 begin
20 readln(n);
21 for i:=1 to n do
22 readln(a[i]);
23 for i:=1 to 5 do
24 for j:=1 to 5 do
25 f[0,i,j]:=1000000;
26 f[0,5,5]:=0;
27 for k:=1 to n do
28 for p:=1 to 5 do
29 for q:=1 to 5 do
30 f[k,p,q]:=maxlongint;
31 for k:=1 to n do
32 for p:=1 to 5 do
33 for q:=1 to 5 do
34 if f[k-1,p,q]<>maxlongint then
35 begin
36 b1:=change(p,a[k]);
37 b2:=change(q,a[k]);
38 f[k,a[k],q]:=min(f[k,a[k],q],f[k-1,p,q]+b1);
39 f[k,a[k],p]:=min(f[k,a[k],p],f[k-1,p,q]+b2);
40 end;
41 minn:=maxlongint;
42 for i:=1 to 5 do
43 for j:=1 to 5 do
44 if f[n,i,j]<minn then minn:=f[n,i,j];
45 writeln(minn);
46 end.

 

描述 Description
怀特先生是一个大胖子。他很喜欢玩跳舞机(Dance Dance Revolution, DDR),甚至希望有一天人家会脚踏“舞蹈家怀特先生”。可惜现在他的动作根本不能称作是在跳舞,尽管每次他都十分投入的表演。这也难怪,有他这样的体型,玩跳舞机是相当费劲的。因此,他希望写一个程序来安排舞步,让他跳起来轻松一些,至少不要每次都汗流浃背。
DDR的主要内容是用脚来踩踏板。踏板有四个方向的箭头,用1 (Up)、2 (Left)、3 (Down)、4 (Right)来代表,中间位置由0来代表。每首歌曲有一个箭头序列,游戏者必须按照或这个序列一次用某一只脚踩相应的踏板。在任何时候,两只脚都不能在同一踏板上,但可以同时待在中心位置0。
每一个时刻,它必须移动而且只能移动他的一只脚去踩相应的箭头,而另一只脚不许移动。跳完一首曲子之后,怀特先生会计算他所消耗的体力。从中心移动到任何一个箭头耗费2单位体力,从任何一个箭头移动到相邻箭头耗费3单位体力,移动到相对的箭头(1和3相对,2和4相对)耗费4单位体力,而留在原地再踩一下只需要1单位。怀特先生应该怎样移动他的双脚(即,对于每个箭头,选一只脚去踩它),才能用最少的体力完成一首给定的舞曲呢?
例如,对于箭头序列Left (2), Left (2), Up (1), Right (4),他应该分别用左、左、右、右脚去踩,总的体力耗费为2+1+2+3=8单位。
输入格式 Input Format
第一行N,表示有N个时刻
第二到n+1行,每行一个数,表示需要踩得版
输出格式 Output Format
一个数,最小消耗体力
样例输入 Sample Input
2 1 1
样例输出 Sample Output
3
时间限制 Time Limitation
各个测试点1s
注释 Hint
n<=10000

posted on 2011-08-31 21:37  kid_jiao  阅读(162)  评论(0)    收藏  举报

导航