題目大意:有一個胖子在玩跳舞機,剛開始的位置在(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
#include
#include
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;
}
版權聲明:本文為博主原創文章,未經博主允許不得轉載。