题目大意:有一个胖子在玩跳舞机。刚開始的位置在(0,0)。跳舞机有四个方向键,上左下右分别相应1,2,3,4.如今有下面规则
1.假设从0位置移动到随意四个位置,消耗能量2
2.假设从非0位置跳到相邻的位置,如1跳到2或4,消耗能量3
3.假设从非0位置跳到对面的位置。如2跳到4。消耗能量4
4.假设跳同一个位置,消耗能量1
5.两仅仅脚不能在同一个位置
解题思路:这题事实上非常水。直接暴力就能够攻克了,讨论全部情况,用dp[i][j][k]表示跳第k个数字。左脚在i这个位置。右脚在j这个位置时所消耗的能量,接着分类讨论
1.假设当中一仅仅脚在0上的情况
2.当中一仅仅脚踩的数字和当前要跳的数字一样
3.两仅仅脚踩的数字和当前的数字不一样
三种情况,分别在细分就可以,详细看代码
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 50010
#define INF 0x3f3f3f3f
int dp[5][5][maxn];
int seq[maxn];
int strength[2] = {4,3};
int n;
int solve() {
memset(dp, 0x3f, sizeof(dp));
dp[0][seq[0]][0] = dp[seq[0]][0][0] = 2;
for(int i = 1; i < n; i++) {
for(int j = 0; j < 5; j++) {
if(dp[j][seq[i-1]][i-1] != INF) {
if(j == 0) {
if(seq[i] != seq[i-1])
dp[seq[i]][seq[i-1]][i] = dp[j][seq[i-1]][i-1] + 2;
if(seq[i] == seq[i-1])
dp[j][seq[i-1]][i] = dp[j][seq[i-1]][i-1] + 1;
else
dp[j][seq[i]][i] = dp[j][seq[i-1]][i-1] + strength[(seq[i-1] + seq[i]) % 2];
}
else if(j == seq[i] || seq[i-1] == seq[i])
dp[j][seq[i-1]][i] = min(dp[j][seq[i-1]][i],dp[j][seq[i-1]][i-1] + 1);
else {
dp[seq[i]][seq[i-1]][i] = min(dp[j][seq[i-1]][i-1] + strength[(j + seq[i]) % 2], dp[seq[i]][seq[i-1]][i]);
dp[j][seq[i]][i] = min(dp[j][seq[i-1]][i-1] + strength[(seq[i-1] + seq[i] ) % 2], dp[j][seq[i]][i]);
}
}
if(dp[seq[i-1]][j][i-1] != INF) {
if(j == 0) {
if(seq[i] != seq[i-1])
dp[seq[i]][seq[i-1]][i] = dp[seq[i-1]][j][i-1] + 2;
if(seq[i] == seq[i-1])
dp[seq[i-1]][j][i] = dp[seq[i-1]][j][i-1] + 1;
else
dp[seq[i]][j][i] = dp[seq[i-1]][j][i-1] + strength[(seq[i-1] + seq[i]) % 2];
}
if(j == seq[i] || seq[i-1] == seq[i])
dp[seq[i-1]][j][i] = min(dp[seq[i-1]][j][i],dp[seq[i-1]][j][i-1] + 1);
else {
dp[seq[i]][seq[i-1]][i] = min(dp[seq[i-1]][j][i-1] + strength[(j + seq[i]) % 2], dp[seq[i]][seq[i-1]][i]);
dp[seq[i]][j][i] = min(dp[seq[i-1]][j][i-1] + strength[(seq[i-1] + seq[i] ) % 2], dp[seq[i]][j][i]);
}
}
}
}
int ans = INF;
for(int i = 0; i < 5; i++)
ans = min(min(ans, dp[seq[n-1]][i][n-1]), dp[i][seq[n-1]][n-1]);
return ans;
}
int main() {
n = 0;
while(scanf("%d", &seq[n]) != EOF && seq[n++]) {
while(scanf("%d", &seq[n]) && seq[n])
n++;
printf("%d
", solve());
n = 0;
}
return 0;
}