bnu8207 Dance Dance Revolution

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<math.h>
 5 #include<algorithm>
 6 using namespace std;
 7 #define left 2
 8 #define right 4
 9 #define up 1
10 #define down 3
11 int n;
12 int a[1000];
13 int effort(int a,int b)
14 {
15     if(a==0)
16     {
17         return 2;
18     }
19     if(a==b)
20     {
21         return 1;
22     }
23     if((a==left&&b==right)||(a==right&&b==left))
24     {
25         return 4;
26     }
27     if((a==up&&b==down)||(a==down&&b==up))
28     {
29         return 4;
30     }
31     return 3;
32 }
33 int d[1000][5][5];
34 int found(int k,int leftfoot,int rightfoot)//当前要执行a[k]步伐
35 {
36     int temp1,temp2;
37     if(k==n-1)
38     {
39         temp1=effort(leftfoot,a[k]);
40         temp2=effort(rightfoot,a[k]);
41         return d[k][leftfoot][rightfoot]=temp1<temp2?temp1:temp2;;
42     }
43     if(d[k+1][leftfoot][a[k]]==-1)
44     {
45         found(k+1,leftfoot,a[k]);
46     }
47     if(d[k+1][a[k]][rightfoot]==-1)
48     {
49         found(k+1,a[k],rightfoot);
50     }
51     temp1=d[k+1][leftfoot][a[k]]+effort(rightfoot,a[k]);
52     temp2=d[k+1][a[k]][rightfoot]+effort(leftfoot,a[k]);
53     return d[k][leftfoot][rightfoot]=temp1>temp2?temp2:temp1;
54 }
55 void init()
56 {
57     int i,j,k;
58     for(i=0;i<n;i++)
59     {
60         for(j=0;j<5;j++)
61         {
62             for(k=0;k<5;k++)
63             {
64                 d[i][j][k]=-1;
65             }
66         }
67     }
68 }
69 int main()
70 {
71     while(scanf("%d",&a[0]),a[0])
72     {
73         n=1;
74         while(scanf("%d",&a[n]),a[n])
75         {
76             n++;
77         } 
78         init();
79         int ans=found(0,0,0);
80         printf("%d\n",ans);
81     }
82     return 0;
83 }
84 //56 ms
posted @ 2012-08-03 14:24  willzhang  阅读(123)  评论(0编辑  收藏  举报