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 跳舞机的更多相关文章

  1. 【暑假】[深入动态规划]UVa 10618 Fun Game

    UVa 10618 Fun Game 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035 思路:   一圈人围坐 ...

  2. 【暑假】[深入动态规划]UVa 10618 Fixing the Great Wall

    UVa 10618 Fixing the Great Wall 题目:  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=361 ...

  3. 【暑假】[深入动态规划]UVa 10618 Tango Tango Insurrection

    UVa 10618 Tango Tango Insurrection 题目: Problem A: Tango Tango Insurrection You are attempting to lea ...

  4. UVA 10618 Tango Tango Insurrection

    https://vjudge.net/problem/UVA-10618 题目 你想学着玩跳舞机.跳舞机的踏板上有4个箭头:上.下.左.右.当舞曲开始时,屏幕上会有一些箭头往上移动.当向上移动箭头与顶 ...

  5. uva 10618 Tango Tango Insurrection 解题报告

    Tango Tango Insurrection Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebu ...

  6. 【Uva 10618】Tango Tango Insurrection

    [Link]: [Description] 玩跳舞机. 有一定的约束. 归纳起来就是以下三点 1.两只脚不能同时踩一个位置 2.如果左脚踩在了右键上,那么下一次移动的一定要是左脚 3.如果右脚踩在了左 ...

  7. 【暑假】[深入动态规划]UVa 10618 The Bookcase

    UVa 12099  The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路:    ...

  8. uva 1291(dp)

    题意:有一台跳舞机,中间是0.上左下右分别代表1 2 3 4,初始状态人站在中间.两仅仅脚都踏在0上,然后给出一段序列以0为结束,要按顺序踩出来,从0踏到四个方向须要消耗2点能量,从一个方向到相邻的方 ...

  9. UVA 1291 Dance Dance Revolution(DP)

    意甲冠军:跳舞机有一个上5积分,分别central, top, bottom, left, right分,区区足站立还是需要1点物理,从一个单纯的脚central点上须要2点体力,从一个点上移动到相邻 ...

随机推荐

  1. CentOS工作内容(七)禁用IPV6

    CentOS工作内容(七)禁用IPV6 用到的快捷键 tab 自动补齐(有不知道的吗) ctrl+a 移动到当前行的开头(a ahead) ctrl+e 移动到当前行的开头(e end) ctrl+u ...

  2. DOM对象与Jquery对象转换

    dom对象的样式是这么加的(js) .style.background = “red”; jquery对象样式是这么加的(jq) .css(“background”,”red”); <div i ...

  3. 使用浏览器,调试js代码

    1:创建html网页和js文件 <!doctype html> <html> <head> <meta charset="utf-8"&g ...

  4. SparkSQL UDF两种注册方式:udf() 和 register()

    调用sqlContext.udf.register() 此时注册的方法 只能在sql()中可见,对DataFrame API不可见 用法:sqlContext.udf.register("m ...

  5. PostgreSQL远程连接配置

    postgresql默认情况下,远程访问不能成功,如果需要允许远程访问,需要修改两个配置文件,说明如下: 1.postgresql.conf 将该文件中的listen_addresses项值设定为“* ...

  6. 软件包管理:rpm命令管理-校验和文件提取

    校验主要用于判断文件是否做了更改 修改标志: 会用-V,会看输出结果即可. 当有误操作,比如删了某一个文件,只需知道他属于哪一个rpm包,可用提取找回覆盖就行.并不把整个rpm包安装,而是提取其中的某 ...

  7. Leetcode: Binary Tree Level Order Transversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  8. 下载mysql的源码包

  9. Java设计模式应用——桥接模式

    性能管理系统中,数据产生后需要经过采集,汇聚,入库三个流程,用户才能查询使用. 采集可以是snmp采集,也可以是ems采集:汇聚可以使storm汇聚,也可以是spark汇聚:入库可以是hdfs入库,也 ...

  10. 使用 Laravel 数据填充生成 中文 测试数据

    Laravel5.4 及更新版本设置了相关的配置项 app.faker_locale,只不过在文档和默认的配置文件中看不到这一参数.相关源码在 Illuminate\Database\Database ...