首页 > 代码库 > LA 2031

LA 2031

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:

 

\epsfbox{p2031.eps}

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

dp
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 
 7 using namespace std;
 8 
 9 vector<int> g;
10 int cost[5][5];
11 int dp[2][5][5];
12 
13 void init() {
14         for(int i = 1; i <= 4; ++i) cost[0][i] = 2;
15         cost[1][2] = cost[2][1] = cost[1][4] = cost[4][1] = 3;
16         cost[2][3] = cost[3][2] = cost[3][4] = cost[4][3] = 3;
17 
18         cost[2][4] = cost[4][2] = 4;
19         cost[1][3] = cost[3][1] = 4;
20 }
21 
22 void solve() {
23         memset(dp[0], -1, sizeof(dp[0]));
24         memset(dp[1], -1, sizeof(dp[1]));
25         dp[0][0][0] = 0;
26 
27         int now = 1 ,last = 0;
28         for(int i = 0; i < g.size(); ++i) {
29                 for(int j = 0; j <= 4; ++j) {
30                         for(int k = 0; k <= 4; ++k) {
31                                 if(dp[last][j][k] != -1) {
32                                         if(j == g[i] || k == g[i]) {
33                                                 dp[now][j][k] = dp[now][j][k] == -1 ?
34                                                             dp[last][j][k] + 1 :
35                                                             min(dp[now][j][k], dp[last][j][k] + 1);
36                                         } else {
37                                                 dp[now][g[i]][k] = dp[now][g[i]][k] == -1 ?
38                                                             dp[last][j][k] + cost[j][g[i]] :
39                                                             min(dp[now][g[i]][k], dp[last][j][k] + cost[j][g[i]]);
40                                                 dp[now][j][g[i]] = dp[now][j][g[i]] == -1 ?
41                                                             dp[last][j][k] + cost[k][g[i]] :
42                                                             min(dp[now][j][g[i]], dp[last][j][k] + cost[k][g[i]]);
43                                         }
44 
45                                 }
46                         }
47                 }
48                 swap(now,last);
49                 memset(dp[now], -1, sizeof(dp[now]));
50         }
51 
52         int ans = -1;
53         for(int i = 0; i <= 4; ++i) {
54                 for(int j = 0; j <= 4; ++j) {
55                         if(dp[last][i][j] == -1) continue;
56                         ans = ans == -1 ? dp[last][i][j] : min(ans, dp[last][i][j]);
57                 }
58         }
59         printf("%d\n",ans);
60 }
61 
62 int main()
63 {
64     //freopen("sw.in", "r", stdin);
65 
66     int ch;
67     init();
68     while(~scanf("%d", &ch) && ch) {
69             g.clear();
70             g.push_back(ch);
71             while(scanf("%d",&ch) && ch) {
72                     g.push_back(ch);
73             }
74 
75             solve();
76     }
77     return 0;
78 }
View Code