zoukankan      html  css  js  c++  java
  • CodeForces 132C Logo Turtle (记忆化搜索)

    Description

    A lot of people associate Logo programming language with turtle graphics. In this case the turtle moves along the straight line and accepts commands "T" ("turn around") and "F" ("move 1 unit forward").

    You are given a list of commands that will be given to the turtle. You have to change exactly n commands from the list (one command can be changed several times). How far from the starting point can the turtle move after it follows all the commands of the modified list?

    Input

    The first line of input contains a string commands — the original list of commands. The string commands contains between 1 and 100 characters, inclusive, and contains only characters "T" and "F".

    The second line contains an integer n (1 ≤ n ≤ 50) — the number of commands you have to change in the list.

    Output

    Output the maximum distance from the starting point to the ending point of the turtle's path. The ending point of the turtle's path is turtle's coordinate after it follows all the commands of the modified list.

    Sample Input

    Input
    FT 1
    Output
    2
    Input
    FFFTFFF 2
    Output
    6

    Hint

    In the first example the best option is to change the second command ("T") to "F" — this way the turtle will cover a distance of 2 units.

    In the second example you have to change two commands. One of the ways to cover maximal distance of 6 units is to change the fourth command and first or last one.

    题解:给一个序列,有TF,F代表机器人向前走一步,T代表机器人转身,再给你一个N问改变N个字母后机器人最多可以走多远,每个字母可以改变多次;刚开始写了下没用记忆化搜索果断超时了,而且还不知道怎么把一个字母改变多次,然后看了下大神的写法,加个数组记忆下就好了,关于字母可以改变多次,直接循环N,N-=2就好了;

    dp[2][MAXN<<1][MAXN][50],MAXN<<1是因为当前位置可能为负数;

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<map>
    #include<string>
    using namespace std;
    const int INF=0x3f3f3f3f;
    #define SI(x) scanf("%d",&x)
    #define PI(x) printf("%d",x)
    #define P_ printf(" ")
    #define mem(x,y) memset(x,y,sizeof(x))
    const int MAXN=110;
    char s[MAXN];
    int dp[2][MAXN<<1][MAXN][52];
    int dfs(int fw,int pos,int l,int n){
        if(n<0)return 0;
        if(!s[pos])
            return n>0?0:abs(l);
        if(dp[fw][l+100][pos][n]!=-1)return dp[fw][l+100][pos][n];
        return dp[fw][l+100][pos][n]=max(dfs(fw,pos+1,fw?l+1:l-1,n-(s[pos]=='T')),dfs(1-fw,pos+1,l,n-(s[pos]=='F')));
    }
    int main(){
        int n;
        while(~scanf("%s",s)){
            SI(n);
            mem(dp,-1);
            int ans=0;
            for(;n>=0;n-=2)ans=max(ans,dfs(1,0,0,n));
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    JAVA写入文本文件
    oracle误删数据闪回
    Myeclipese :Creation of element failed解决方法
    Hibernate的四种状态
    java中list、set和map 的区别<转>
    C#操作mysql中临时表不自动删除
    WPF 实现地图的移动和滚动放大
    c# 将十六进制字符串写入注册表
    ASP.NET 视图状态概述:初步了解
    vs好用插件
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5255461.html
Copyright © 2011-2022 走看看