【Uva 10618】Tango Tango Insurrection

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;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626183.html