UVa 10618 跳舞机
https://vjudge.net/problem/UVA-10618
这道题目题意很复杂,代码也是参考了别人的,因为自己实在是写不出。d[i][a][b][s]表示分析到第i个箭头时,此时左脚处于a,右脚处于b,上次移动的脚为s时的最小能量消耗。
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; #define INF 10000000 char str[];
int len;
int d[][][][];
int action[][][][];
int pos[];
char place[] = ".LR"; int energy(int a, int ta)
{
if (a == ta) return ; //该脚没有移动
if (a + ta == ) return ; //该脚移动到相对箭头
return ; //该脚移动到相邻箭头
} int energy(int a, int b, int s, int f, int t, int& ta, int& tb)
{
ta = a;
tb = b;
//移动之后的脚的位置
if (f == ) //左脚
ta = t;
if (f == ) //右脚
tb = t; if (ta == tb) return -; //移动之后两只脚在同一位置
if (ta == && tb == ) return -; //背向跳舞机状态
if (a == && tb != b) return -; //左脚在右箭头时,不能移动右脚
if (b == && ta != a) return -; //右脚在左箭头时,不能移动左脚 int e = ;
if (f == ) //没有移动
e = ;
else if (f != s) //和上个周期移动的脚不同
e = ;
else
{
if (f == )
e = energy(a, ta);
else
e = energy(b, tb);
}
return e;
} void update(int i, int a, int b, int s, int f, int t)
{
int ta, tb;
int e = energy(a, b, s, f, t, ta, tb);
if (e < ) return;
//逆推
int cost = d[i + ][ta][tb][f] + e;
int& ans = d[i][a][b][s];
if (ans>cost)
{
ans = cost;
action[i][a][b][s] = f * + t;
}
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
pos['U'] = ;
pos['D'] = ;
pos['L'] = ;
pos['R'] = ;
while (cin >> str)
{
if (str[] == '#') break;
len = strlen(str);
memset(d, , sizeof(d)); //逆推导
for (int i = len - ; i >= ; i--)
{
for (int a = ; a < ; a++)
for (int b = ; b < ; b++)
{
//两只脚不可能在同一箭头上
if (a == b) continue;
for (int s = ; s < ; s++)
{
d[i][a][b][s] = INF;
if (str[i] == '.')
{
//不移动
update(i, a, b, s, , );
//任意往4个位置移动
for (int t = ; t < ; t++)
{
update(i, a, b, s, , t);
update(i, a, b, s, , t);
}
}
else
{
update(i, a, b, s, , pos[str[i]]); //左脚移动到指定位置
update(i, a, b, s, , pos[str[i]]); //右脚移动到指定位置
}
}
}
}
//初始时左脚在左箭头,右脚在右箭头
int a = , b = , s = ;
for (int i = ; i < len; i++)
{
int f = action[i][a][b][s] / ;
int t = action[i][a][b][s] % ;
cout << place[f];
s = f;
if (f == )
a = t;
else if (f == )
b = t;
}
cout << endl;
}
}
UVa 10618 跳舞机的更多相关文章
- 【暑假】[深入动态规划]UVa 10618 Fun Game
UVa 10618 Fun Game 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035 思路: 一圈人围坐 ...
- 【暑假】[深入动态规划]UVa 10618 Fixing the Great Wall
UVa 10618 Fixing the Great Wall 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=361 ...
- 【暑假】[深入动态规划]UVa 10618 Tango Tango Insurrection
UVa 10618 Tango Tango Insurrection 题目: Problem A: Tango Tango Insurrection You are attempting to lea ...
- UVA 10618 Tango Tango Insurrection
https://vjudge.net/problem/UVA-10618 题目 你想学着玩跳舞机.跳舞机的踏板上有4个箭头:上.下.左.右.当舞曲开始时,屏幕上会有一些箭头往上移动.当向上移动箭头与顶 ...
- uva 10618 Tango Tango Insurrection 解题报告
Tango Tango Insurrection Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebu ...
- 【Uva 10618】Tango Tango Insurrection
[Link]: [Description] 玩跳舞机. 有一定的约束. 归纳起来就是以下三点 1.两只脚不能同时踩一个位置 2.如果左脚踩在了右键上,那么下一次移动的一定要是左脚 3.如果右脚踩在了左 ...
- 【暑假】[深入动态规划]UVa 10618 The Bookcase
UVa 12099 The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路: ...
- uva 1291(dp)
题意:有一台跳舞机,中间是0.上左下右分别代表1 2 3 4,初始状态人站在中间.两仅仅脚都踏在0上,然后给出一段序列以0为结束,要按顺序踩出来,从0踏到四个方向须要消耗2点能量,从一个方向到相邻的方 ...
- UVA 1291 Dance Dance Revolution(DP)
意甲冠军:跳舞机有一个上5积分,分别central, top, bottom, left, right分,区区足站立还是需要1点物理,从一个单纯的脚central点上须要2点体力,从一个点上移动到相邻 ...
随机推荐
- better-scroll一个好用的页面滑动工具
1.npm install better-scroll 2.引入:import BetterScrol from 'better-scroll' 3. 在需要设置页面滚动的地方添加 ref=&qu ...
- [vue]webpack&vue组件工程化实践
[vue]全局组件和局部组件(嵌套+props引用父组件数据) [vue]组件篇 [vue]组件的创建(componet)和销毁(keep-alive缓存)和父子dom同步nextTick [vue] ...
- 微信小程序----团购或秒杀的批量倒计时实现
效果图 实现思路微信小程序实现倒计时,可以将倒计时的时间进行每一秒的计算和渲染! JS模拟商品列表数据 goodsList:在 onLoad 周期函数中对活动结束时间进行提取:建立时间格式化函数 ti ...
- [LeetCode] 429. N-ary Tree Level Order Traversal_ Easy
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Summary: sorting Algorithms
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item a ...
- Vue.js学习笔记——表单控件实践
最近项目中使用了vue替代繁琐的jquery处理dom的数据更新,个人非常喜欢,所以就上官网小小地实践了一把. 以下为表单控件的实践,代码敬上,直接新建html文件,粘贴复制即可看到效果~ <! ...
- FastDFS+nginx+keepalived集群搭建
安装环境 nginx-1.6.2 libfastcommon-master.zip FastDFS_v5.05.tar.gz(http://sourceforge.net/projects/fastd ...
- vue 渲染页面的时候 出现闪烁问题的解决办法
在使用vue绑定数据的时候,渲染页面时会出现变量闪烁 <div id="h_cameraman" v-cloak> <public-nav> {{ msg ...
- Linux基础命令---sum,cksum
cksum 检查文件的crc是否正确,统计文件的字节数. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 cks ...
- linux系统启动顺序及init模式
磁盘的第一个扇区(512bytes)主要记录了两个重要信息: 主引导分区MBR:master boot record,安装引导加载程序的地方,446bytes 分区表:partition table: ...