【Link】:

【Description】



玩跳舞机.

有一定的约束.

归纳起来就是以下三点

1.两只脚不能同时踩一个位置

2.如果左脚踩在了右键上,那么下一次移动的一定要是左脚

3.如果右脚踩在了左键上,那么下一次移动的一定要是右脚

这3个规则和题目所要求的题意等价.

点号的时候,可以任意移动你的脚,或者不移动脚,不移动脚的话就不会产生任何体力.

问你完成所给的游戏序列,最少需要耗费多少体力.

【Solution】



设f[i][a][b][s]表示完成了游戏序列的前i-1个,且完成第i-1个之后,左右脚的位置分别在a和b,以及上一次用的是脚s移动的最小值;

s为0表示没有任何一只脚移动,否则为1表示左脚,为2表示右脚;

则对于第i个游戏序列;

如果s[i]==’.’

则可以选择不用脚做任何移动;

或者选择两只脚中的一只,移动到上下左右4个位置中的一个

(可以写个函数判断移动的合法性,根据上一轮左右脚以及这一轮的左右脚)

如果s[i]!=’.’

则选择两只脚中的一只,移动到所需要的位置;

至于每次移动的花费

根据移动的那只脚的前后位置,以及上一轮和这一轮移动的脚

记录方案输出即可.



【NumberOf WA】



0



【Reviw】



题意有点迷…

这种用递归里面的参数来辨别序号的方法很好用

(这题,序号指的是左和右)



【Code】

#include <bits/stdc++.h>
using namespace std;
const int N = 70;
const int INF = 0x3f3f3f3f; struct abc{
int a,b,s;
}; char S[N+20];
int n,idx[300];
int f[N+20][5][5][5];
abc path[N+20][5][5][5]; void pre(){
idx['U'] = 2,idx['D'] = 1;
idx['L'] = 0,idx['R'] = 3;
} bool ok(int a,int b,int pa,int pb){
if (a==pa && b==pb)
return true;
else
if (a==pa)
return (pb!=a && a!= 3);
else
if (b==pb)
return (pa!=b && b!=0);
return false;
} int cost(int pa,int a,int ps,int s){
if (ps!=s) return 1;
if (pa == a) return 3;
if (pa + a==3) return 7;
return 5;
} int dfs(int i,int a,int b,int s){
if (i==n+1) return 0;
if (f[i][a][b][s]!=INF) return f[i][a][b][s];
int &t = f[i][a][b][s],temp;
abc &p = path[i][a][b][s];
if (S[i]=='.'){
t = dfs(i+1,a,b,0);
p.a = a,p.b = b,p.s = 0;
for (int j = 0;j <= 3;j++)
{
if (ok(a,b,j,b)){
temp = dfs(i+1,j,b,1) + cost(a,j,s,1);
if (temp < t){
t = temp;
p.a = j,p.b = b,p.s = 1;
}
} if (ok(a,b,a,j)){
temp = dfs(i+1,a,j,2) + cost(b,j,s,2);
if (temp < t){
t = temp;
p.a = a,p.b = j,p.s = 2;
}
}
}
}else{
int pos = idx[ (int) S[i]];
if (ok(a,b,pos,b)){
temp = dfs(i+1,pos,b,1) + cost(a,pos,s,1);
if (temp < t){
t = temp;
p.a = pos,p.b = b,p.s = 1;
}
} if (ok(a,b,a,pos)){
temp = dfs(i+1,a,pos,2) + cost(b,pos,s,2);
if (temp < t){
t = temp;
p.a = a,p.b = pos,p.s = 2;
}
}
}
return t;
} void print(int i,int a,int b,int s){
if (i==n+1) return;
int t = path[i][a][b][s].s;
if (t==0) putchar('.');
if (t==1) putchar('L');
if (t==2) putchar('R');
print(i+1,path[i][a][b][s].a,path[i][a][b][s].b,
path[i][a][b][s].s);
} int main(){
//freopen("D:\\rush.txt","r",stdin);
pre();
while (~scanf("%s",S+1)){
memset(f,INF,sizeof f);
memset(path,0,sizeof path);
if (S[1] == '#') break;
n = strlen(S+1);
dfs(1,0,3,0);
print(1,0,3,0);
puts("");
}
return 0;
}

【Uva 10618】Tango Tango Insurrection的更多相关文章

  1. 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵

    偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...

  2. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  3. 【UVa 10881】Piotr's Ants

    Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...

  4. 【UVa 116】Unidirectional TSP

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. 【UVa 1347】Tour

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. 【UVA 437】The Tower of Babylon(记忆化搜索写法)

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. 【uva 1025】A Spy in the Metro

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. 【Uva 11584】Partitioning by Palindromes

    [Link]:https://cn.vjudge.net/contest/170078#problem/G [Description] 给你若干个只由小写字母组成的字符串; 问你,这个字符串,最少能由 ...

  9. 【Uva 11400】Lighting System Design

    [Link]: [Description] 你要构建一个供电系统; 给你n种灯泡来构建这么一个系统; 每种灯泡有4个参数 1.灯泡的工作电压 2.灯泡的所需的电源的花费(只要买一个电源就能供这种灯泡的 ...

随机推荐

  1. hdu 2037 贪心

    今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  2. 数位DP CF388D - Fox and Perfect Sets

    题目地址 一个整数perfect集合满足性质:集合中随意两个整数的异或和仍在这个集合中. 求最大数不超过K的perfect集合的个数. 每一个集合都是一个线性的向量空间. .能够通过全然的高斯消元得出 ...

  3. so near yet so far

    Dear little yang So beautiful boy as you, the most beautiful boy is you who i ever saw, like a sun , ...

  4. less10 loop循环

    less .loop(@counter) when (@counter > 0) { .loop((@counter - 1)); // 递归调用自身 4 3 2 1 0 width: (10p ...

  5. hdu_1394,线段树求逆序数

    http://www.notonlysuccess.com/index.php/segment-tree-complete/ #include<iostream> #include< ...

  6. 使用Visual Studio2012调试Redis源码

    Redis是一款C语言编写Key-Value存储系统,基于BSD协议开放源码,其源码托管在github上,大概有三万行. 源码地址:https://github.com/antirez/redis 源 ...

  7. Mojom IDL and Bindings Generator

    Mojom IDL and Bindings Generator This document is a subset of the Mojo documentation. Contents Overv ...

  8. c3p0-config.xml

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <c3p0-confi ...

  9. 由于webpack-cli版本问题造成的错误

    The CLI moved into a separate package: webpack-cli Please install 'webpack-cli' in addition to webpa ...

  10. HDU-1225 Football Score 模拟问题(水题)

    题目链接:https://cn.vjudge.net/problem/HDU-1225 水题 代码 #include <algorithm> #include <string> ...