UVA1291----Dance Dance Revolution----3维DP
本文出自: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的更多相关文章
- 递推DP UVA 1291 Dance Dance Revolution
题目传送门 题意:给一串跳舞的动作,至少一只脚落到指定的位置,不同的走法有不同的体力消耗,问最小体力消费多少分析:dp[i][j][k] 表示前i个动作,当前状态(j, k)的最小消费,状态转移方程: ...
- UVA 1291 十四 Dance Dance Revolution
Dance Dance Revolution Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
- [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 ...
- Dance Dance Revolution
今天我们来讲 Dance Dance Revolution这题 本题原网址 注意本题为多组输入输出,直到输入单个零而止(题面有点小问题) 很明显,此题为一道动态规划题(请不要妄想用贪心算法过这题,尽管 ...
- 悦动达人 (多维dp)
悦动达人 Description 一个游戏,在屏幕上有5个格子形成一行,每一秒都会有一个格子闪烁,格子闪烁时你需要保证至少有一只手指在格子上面, 现在我们已经知道第i秒时,第xi个格子会闪烁,我们假设 ...
- POJ - 1170 Shopping Offers (五维DP)
题目大意:有一个人要买b件商品,给出每件商品的编号,价格和数量,恰逢商店打折.有s种打折方式.问怎么才干使买的价格达到最低 解题思路:最多仅仅有五种商品.且每件商品最多仅仅有5个,所以能够用5维dp来 ...
- luogu 4401 矿工配餐 多维dp
五维dp,记忆化搜索会MLE超内存,所以用滚动数组,十分经典 五维dp #include <bits/stdc++.h> using namespace std; ; ][][][],la ...
- 洛谷p1732 活蹦乱跳的香穗子 二维DP
今天不BB了,直接帖原题吧 地址>>https://www.luogu.org/problem/show?pid=1732<< 题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现 ...
- 洛谷 P1006 传纸条 多维DP
传纸条详解: 蒟蒻最近接到了练习DP的通知,于是跑来试炼场看看:发现有点难(毕竟是蒟蒻吗)便去翻了翻题解,可怎么都看不懂.为什么呢?蒟蒻发现题解里都非常详细的讲了转移方程,讲了降维优化,但这题新颖之处 ...
- Blocks POJ - 1390 多维dp
题意:有一排box,各有不同的颜色.你可以通过点击某个box使得与其相邻的同色box全部消掉,然后你可以得到的分数为消去长度的平方,问怎样得到最高分? 题解:考虑用一维dp,/*dp[i]为1~i个b ...
随机推荐
- Highcharts使用手册
chart: { type: 'area', ignoreHiddenSeries: false, //如果true,一旦一个系列被隐藏,轴将会扩展剩余的可见系列 }, 这是设置的两个纵坐标轴: yA ...
- javascript 中字符串之比较
<script type="text/javascript"> var string1="apple"; var string2="Ban ...
- codeforces 645C . Enduring Exodus 三分
题目链接 我们将所有为0的位置的下标存起来. 然后我们枚举左端点i, 那么i+k就是右端点. 然后我们三分John的位置, 找到下标为i时的最小值. 复杂度 $ O(nlogn) $ #include ...
- s3c6410学习笔记-将内核zImage、文件系统写到nandflash、屏幕校准
1.之前已经将uboot写到nandflash里面了,接下来将内核zImage.文件系统写到nandflash. 2.编译内核 cd linux-2.6.28_smdk6410 make clean ...
- linux开机自动挂载NTFS-WINDOWS分区
1.安装ntfs-3g-2009.4.4.tgz 2.输入fdisk -l 看一下分区 由此可见:/dev/sda5,6,7 即是windows下的D,E,F盘(NTFS格式). 3.vim /etc ...
- LINUX常用命令-系统配置篇(二)
学到一定程度了就会关注系统方面的一些配置,只是就需要相关的命令了.现在把Linux查看系统配置常用命令列出来 # uname -a # 查看内核/操作系统/CPU信息# head -n 1 /etc/ ...
- “_In_opt_z_”: 未声明的标识符
问题 使用VS2010 + WDK 环境进行驱动开发时,编译阶段出现如下等错误提示 “_In_opt_z_”: 未声明的标识符 解决办法 将..\Microsoft Visual Studio 10. ...
- 「python」: arp脚本的两种方法
「python」: arp脚本的两种方法 第一种是使用arping工具: #!/usr/bin/env python import subprocess import sys import re de ...
- Openstack service default port
Block Storage (cinder) 8776 publicurl and adminurl Compute API (nova-api) 8773 EC2 API 8774 openstac ...
- socketio 握手前中断报错
前两天折腾了下socketio,部署完发现通过nginx代理之后前端的socket无法和后端通信了,于是暴查一通,最后解决问题: location / { proxy_pass http://127. ...