标签:
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
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.
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.
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.
1 2 2 4 0 1 2 3 4 1 2 3 3 4 2 0 0
8 22
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 const int inf=0x3f3f3f3f; 6 7 int a[10005]; 8 int dp[10005][5][5]; 9 int main() 10 { 11 int n; 12 int i,j,k; 13 while(scanf("%d",&a[0])!=EOF && a[0]!=0) 14 { 15 n=0; 16 while(a[n]!=0) 17 { 18 n++; 19 scanf("%d",&a[n]); 20 } 21 memset(dp,inf,sizeof(dp)); 22 dp[0][a[0]][0]=2,dp[0][0][a[0]]=2; 23 for(i=1;i<n;i++) 24 { 25 for(j=0;j<=4;j++) 26 { 27 for(k=0;k<=4;k++) 28 { 29 if(dp[i-1][j][k]<inf) 30 { 31 int x,y; 32 if(a[i]!=k) 33 { 34 x=abs(a[i]-j); 35 if(x==0) 36 y=1; 37 else if(x==1 || x==3) 38 y=3; 39 else if(x==2) 40 y=4; 41 if(j==0) 42 y=2; 43 dp[i][a[i]][k]=min(dp[i][a[i]][k],dp[i-1][j][k]+y); 44 } 45 if(a[i]!=j) 46 { 47 x=abs(a[i]-k); 48 if(x==0) 49 y=1; 50 else if(x==1 || x==3) 51 y=3; 52 else if(x==2) 53 y=4; 54 if(k==0) 55 y=2; 56 dp[i][j][a[i]]=min(dp[i][j][a[i]],dp[i-1][j][k]+y); 57 } 58 } 59 } 60 } 61 } 62 63 int ans=inf; 64 for(i=0;i<=4;i++) 65 { 66 if(dp[n-1][i][a[n-1]]<ans) 67 ans=dp[n-1][i][a[n-1]]; 68 if(dp[n-1][a[n-1]][i]<ans) 69 ans=dp[n-1][i][a[n-1]]; 70 } 71 printf("%d\n",ans); 72 } 73 return 0; 74 }
UVA 1291 十四 Dance Dance Revolution
标签:
原文地址:http://www.cnblogs.com/cyd308/p/4771604.html