标签:
题目大意:有一个胖子在玩跳舞机,刚开始的位置在(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\n", solve());
n = 0;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
UVALive - 2031 Dance Dance Revolution 三维dp
标签:
原文地址:http://blog.csdn.net/l123012013048/article/details/46852101