本文出自:http://blog.csdn.net/dr5459

题目地址:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4037

题目意思:

跳舞机

中间为0

上左下右分别为1,2,3,4

然后从0到其他消费2

相邻的移动消费3

原地踏步消费1

相对移动消费2

给你一串舞步,初始双脚站在中间,问你跳完的最小消耗

思路:

简单的区间DP,但是自己写了好久啊,囧

令f[n][i][j]表示第n步时的左右脚分别为i,j的最小步数

则装态转移方程:

如果f(i, j, s), (0<=j<=4)状态可达
则可推出下一个的状态
f(i+1, j, s) = f(i, j, s) + 1; // 停在当前不动
f(i+1, next, s) = min{ f(i, j, s) + check(j, next)}
f(i+1, j, next) = min{ f(i, j, s) + check(s, next)}

同理,如果f(i, s, j), (0<=j<=4)状态可达
也可推出下一个状态:
f(i+1, s, j) = f(i, j, s) + 1; // 停在原地不动
f(i+1, next, j) = min{ f(i, s, j) + check(s, next)}
f(i+1, s, next) = min{ f(i, s, j) + check(j, next)}

推了很久啊

下面上代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int dp[20000][5][5];
const int inf = 0x3f3f3f3f; int check(int st,int ed)
{
if(st==ed)
return 1;
if(st==0 || ed==0)
return 2;
if(st==1)
{
if(ed == 2 || ed==4)
return 3;
return 4;
} if(st==2)
{
if(ed == 1 || ed==3)
return 3;
return 4;
} if(st == 3)
{
if(ed==2 || ed==4)
return 3;
return 4;
} if(st==4)
{
if(ed == 1|| ed==3)
return 3;
return 4;
}
} int main()
{
int tmp;
while(1)
{
int cnt = 0;
memset(dp,inf,sizeof(dp));
dp[0][0][0] = 0;
int tmp2=0;
while(1)
{
scanf("%d",&tmp);
if(tmp == 0)
break;
cnt++;
for(int i=0;i<=4;i++)
{
dp[cnt][i][tmp2] = min(dp[cnt][i][tmp2],dp[cnt-1][i][tmp2]+1);
dp[cnt][tmp2][i] = min(dp[cnt][tmp2][i],dp[cnt-1][tmp2][i]+1);
dp[cnt][tmp][i] = min(dp[cnt][tmp][i],dp[cnt-1][tmp2][i]+check(tmp2,tmp));
dp[cnt][tmp2][tmp] = min(dp[cnt][tmp2][tmp],dp[cnt-1][tmp2][i]+check(i,tmp));
dp[cnt][tmp][tmp2] = min(dp[cnt][tmp][tmp2],dp[cnt-1][i][tmp2]+check(i,tmp));
dp[cnt][i][tmp] = min(dp[cnt][i][tmp],dp[cnt-1][i][tmp2]+check(tmp2,tmp));
}
tmp2 = tmp;
}
if(cnt==0)
break;
int ans = inf;
for(int i=0;i<=4;i++) if(i!=tmp2)
ans = min(dp[cnt][tmp2][i],ans);
for(int i=0;i<=4;i++) if(i!=tmp2)
ans = min(dp[cnt][i][tmp2],ans);
cout<<ans<<endl;
}
return 0;
}

UVA1291----Dance Dance Revolution----3维DP的更多相关文章

  1. 递推DP UVA 1291 Dance Dance Revolution

    题目传送门 题意:给一串跳舞的动作,至少一只脚落到指定的位置,不同的走法有不同的体力消耗,问最小体力消费多少分析:dp[i][j][k] 表示前i个动作,当前状态(j, k)的最小消费,状态转移方程: ...

  2. UVA 1291 十四 Dance Dance Revolution

    Dance Dance Revolution Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Su ...

  3. [LA] 2031 Dance Dance Revolution

    Dance Dance Revolution Time limit: 3.000 seconds Mr. White, a fat man, now is crazy about a game nam ...

  4. Dance Dance Revolution

    今天我们来讲 Dance Dance Revolution这题 本题原网址 注意本题为多组输入输出,直到输入单个零而止(题面有点小问题) 很明显,此题为一道动态规划题(请不要妄想用贪心算法过这题,尽管 ...

  5. 悦动达人 (多维dp)

    悦动达人 Description 一个游戏,在屏幕上有5个格子形成一行,每一秒都会有一个格子闪烁,格子闪烁时你需要保证至少有一只手指在格子上面, 现在我们已经知道第i秒时,第xi个格子会闪烁,我们假设 ...

  6. POJ - 1170 Shopping Offers (五维DP)

    题目大意:有一个人要买b件商品,给出每件商品的编号,价格和数量,恰逢商店打折.有s种打折方式.问怎么才干使买的价格达到最低 解题思路:最多仅仅有五种商品.且每件商品最多仅仅有5个,所以能够用5维dp来 ...

  7. luogu 4401 矿工配餐 多维dp

    五维dp,记忆化搜索会MLE超内存,所以用滚动数组,十分经典 五维dp #include <bits/stdc++.h> using namespace std; ; ][][][],la ...

  8. 洛谷p1732 活蹦乱跳的香穗子 二维DP

    今天不BB了,直接帖原题吧  地址>>https://www.luogu.org/problem/show?pid=1732<< 题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现 ...

  9. 洛谷 P1006 传纸条 多维DP

    传纸条详解: 蒟蒻最近接到了练习DP的通知,于是跑来试炼场看看:发现有点难(毕竟是蒟蒻吗)便去翻了翻题解,可怎么都看不懂.为什么呢?蒟蒻发现题解里都非常详细的讲了转移方程,讲了降维优化,但这题新颖之处 ...

  10. Blocks POJ - 1390 多维dp

    题意:有一排box,各有不同的颜色.你可以通过点击某个box使得与其相邻的同色box全部消掉,然后你可以得到的分数为消去长度的平方,问怎样得到最高分? 题解:考虑用一维dp,/*dp[i]为1~i个b ...

随机推荐

  1. Linux网络管理——端口作用

    1. 网络基础 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",&q ...

  2. javascript自定义日期函数

    1.格式化日期(YYYY-MM-DD) 代码: var DateFormat = function (date) { if (!(date instanceof Date)) { date = dat ...

  3. 联系InfoSphere Streams和OpenMI时对水利模型联系的设计模式的一些考虑

    从<时序计算通用模型接口 OpenMI开发技术及应用>一书中的第一章的对接口要求描述,我想到InfoSphere streams的流数据处理模式刚好可以满足这种模型/数据之间对接的需求. ...

  4. poj 2723 Get Luffy Out 二分+2-sat

    题目链接 给n个钥匙对, 每个钥匙对里有两个钥匙, 并且只能选择一个. 有m扇门, 每个门上有两个锁, 只要打开其中一个就可以通往下一扇门. 问你最多可以打开多少个门. 对于每个钥匙对, 如果选择了其 ...

  5. PowerShell入门(一):PowerShell能干什么?

    原文链接:http://www.cnblogs.com/ceachy/archive/2013/01/30/WhatCanPowerShellDo.html PowerShell能干什么呢?就像序言中 ...

  6. 谈谈文件增量同步算法:RSYNC和CDC

    谈谈文件增量同步算法:RSYNC和CDC 分类: 数据同步 增量备份 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在研究文件的增量同步问题,着重研究了文件差异编码部分,因为这个其实是文件 ...

  7. Hibernate get 和load的区别

    1 load是要用的时候才从数据库去查询,get 是马上查询. 2 对于不存在的记录,get会报空指针异常,load会报 org.hibernate.ObjectNotFoundException:  ...

  8. 轻奢品牌全面崛起 Coach、UGG等纷纷抢滩新兴市场_新闻中心_赢商网

    轻奢品牌全面崛起 Coach.UGG等纷纷抢滩新兴市场_新闻中心_赢商网 轻奢品牌全面崛起 Coach.UGG等纷纷抢滩新兴市场

  9. javascript数组顺序-----1冒泡的另一种比较好理解的写法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 从ACM中删除一个已经创建的Library

    从ACM中删除一个已经创建的Library,无法通过界面操作,须要手工从DB中删除.须要删除的表记录有: RECENTUPDATE 找到字段Name等于该libraryName的那条记录删除掉 del ...