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点体力,从一个点上移动到相邻 ...
随机推荐
- Linux Packages Search
网站 : https://www.pkgs.org/ https://centos.pkgs.org/
- 各版本 MySQL 并行复制的实现及优缺点
MySQL并行复制已经是老生常谈,笔者从2010年开始就着手处理线上这个问题,刚开始两三年也乐此不疲分享,现在再提这个话题本来是难免“炒冷饭”嫌疑. 最近触发再谈这个话题,是因为有些同学觉得“5.7的 ...
- [py]字符串/列表
去除str首尾空格(切片) ## str长度 循环,判断 ### [:i] [i:] 记录位置点 ## 方法1 def trim2(s): s2 = "" start = 0 en ...
- [LeetCode] 301. Remove Invalid Parentheses_Hard tag:BFS
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- jmeter 正则表达式提取器的使用(提取第一个匹配结果)
原文地址https://www.cnblogs.com/xueli/p/7405258.html?utm_source=itdadao&utm_medium=referral 正则表达式的用处 ...
- python3 集合的常用方法
方法 意义 S.add(e) 在集合中添加一个新的元素e:如果元素已经存在,则不添加 S.remove(e) 从集合中删除一个元素,如果元素不存在于集合中,则会产生一个KeyError错误 S.dis ...
- quartz-job实现实时或定时发送短信任务
存放调度器(Job 和 Trigger)信息的xml配置文件: 这是某个指定的要实现的定时任务: <!-- 每天给项目经理发送短信避免短信服务挂了 定时每天08:30执行--> <j ...
- Js基础知识1-数组操作全解
数组操作全解 js变量类型 var string; var name = "student",age=12; //underfined.null.boolean.string.nu ...
- cursor图标自定义
cursor: url(./images/favicon.ico), auto; 首先auto必须加上,其次必须使用ico文件,目前来说ico文件没有兼容性问题,ico格式怎么转? 传送门:http: ...
- FreeModbus移植实例(转)
源:分享FreeRTOS + FreeModbus + UART_RTO FREERTOS 移植学习 入门必备 正点原子官方所有开发板的FreeRTOS教程及其例程正式发布(STM32F103,STM ...