zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #96 (Div. 1) C. Logo Turtle DP

    C. Logo Turtle
     

    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.

    Examples
    input
    FT
    1
    output
    2
    Note

    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.

    题意:

      给你一串由FT构成的串,F代表前进,T代表转向,初始方向是1,转向后F由1变为-1(或者-1变成1)

      给你一个n,意思是你也可以在任意位置的字符改变任意次数但总和不要超过n次的情况下,其状态即F变T,T变F;

      问你改变n次后,最后机器人能走到的最远距离

    题解:

      范围很小

      设定DP[i][j][k][0/1]表示在走完i个字符使用j次变化,在k位置,方向0/1是否能够达成

      算好复杂度,无脑暴力怼它的转移.

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    using namespace std;
    typedef long long LL;
    const int N=1e6+10,mod=20090717,inf=2e9+10;
    
    int dp[110][55][310][2],m,n;
    char a[N];
    int main() {
        scanf("%s%d",a+1,&m);
        n = strlen(a+1);
        dp[0][0][100][1] = 1;
        for(int i = 0; i < n; ++i) {
             for(int j = 0; j <= m; ++j) {
                for(int k = 0; k < 210; ++k) {
                    for(int cnt = 0; cnt <= m - j; ++cnt) {
                        if(a[i+1] == 'T') {
                            if(cnt%2) {
                                dp[i+1][j+cnt][k+1][1] |= dp[i][j][k][1];
                                if(k-1>=0)dp[i+1][j+cnt][k-1][0] |= dp[i][j][k][0];
                            }
                            else {
                                dp[i+1][j+cnt][k][1] |= dp[i][j][k][0];
                                dp[i+1][j+cnt][k][0] |= dp[i][j][k][1];
                            }
                        }
                        else {
                            if(cnt%2) {
                                dp[i+1][j+cnt][k][1] |= dp[i][j][k][0];
                                dp[i+1][j+cnt][k][0] |= dp[i][j][k][1];
                            }
                            else {
                                dp[i+1][j+cnt][k+1][1] |= dp[i][j][k][1];
                                if(k-1>=0)dp[i+1][j+cnt][k-1][0] |= dp[i][j][k][0];
                            }
                        }
                    }
                }
             }
        }
        int ans = 0;
        for(int j = 0; j < 210; ++j) {
            if(dp[n][m][j][0]) ans = max(ans,abs(j-100));
            if(dp[n][m][j][1]) ans = max(ans,abs(j-100));
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    .NET Framework 1.13.5 版本安装包下载链接
    可遇不可求的Question之MYSQL获取自增ID的四种方法篇
    20110917 晴
    北海道 7天6夜 自助游
    想你了
    猫忘带电话了
    [转载经验] 探亲签证申请
    帮忙打印
    打印机
    20110910 晴
  • 原文地址:https://www.cnblogs.com/zxhl/p/6543531.html
Copyright © 2011-2022 走看看