Codeforces 132C Logo Turtle (DP)

Source: Internet
Author: User

You can use a three-dimensional DP to save the state, dp[i][j][k] means that the first I character after the J-Step direction is k (k = 1 or k = 0) The optimal solution, that is, the maximum distance from the origin point. Here the 0 direction is the positive direction, 1 negative direction, indicating the current direction of the person. These two directions are opposite.

So you can recursively push a relationship, when the I-character is ' F ' or ' T '

If the ' F '

The enumeration, in turn, transforms a few steps in the first place, which is the scope of the enumeration for 0~J, assuming that the K-step (and the dp[i][j][k above) is not one

1. If k is an odd number, it is equivalent to a change of 1 steps, so ' F ' becomes ' T ', then his direction also changed. So the current direction must be the opposite of the previous step (that is, the direction of i-1), so there is dp[i][j][0] = max (dp[i][j][0], dp[i-1][j-k][1]), similarly, dp[i][j][1] = max (dp[i][j][1], Dp[i-1][j-k][0])

2. If k is even, the equivalent of no change, so still the character ' F ', if it is the positive direction, then he can continue to the positive direction from the previous step, that is, add 1, if the negative direction, the equivalent of a step back, the distance is reduced by 1, the recursive equation is dp[i][j][0] = max (dp[i][j ][0], dp[i-1][j-k][0] + 1); DP[I][J][1] = max (dp[i][j][1], dp[i-1][j-k][1]-1);

If the ' T '

The enumeration in the first place changed several steps, the range is also 0~j, assuming the transformation K-step

1. If k is odd, then it changes to ' F ', so dp[i][j][0] = max (dp[i][j][0], dp[i-1][j-k][0] + 1); DP[I][J][1] = max (dp[i][j][1], dp[i-1][j-k][1]-1)

2. If k is even, then ' T ', so dp[i][j][0] = max (dp[i][j][0], dp[i-1][j-k][1]); DP[I][J][1] = max (dp[i][j][1], dp[i-1][j-k][0])

One of the problems is that it has nothing to do with the direction of the beginning, regardless of the direction, as long as the furthest walk, and there are always two opposing directions. Initialization dp[0][0][0] and dp[0][0][1] all have to initialize 0 so that you can go anywhere

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN = the;intdp[maxn][maxn][2];Const intINF =0x3f3f3f3f;intMain () {//freopen ("In.txt", "R", stdin);    CharCMD[MAXN]; scanf ("%s", CMD +1); intN; scanf ("%d", &N); intLen = strlen (cmd +1); //memset (DP,-inf, sizeof (DP));     for(inti =0; I <= Len; i++)         for(intj =0; J <= N; J + +) dp[i][j][0] = dp[i][j][1] = -inf; dp[0][0][0] =0;//Positive Directiondp[0][0][1] =0;//negative direction, both sides can walk     for(inti =1; I <= Len; i++)    {         for(intj =0; J <= N; J + +)        {             for(intK =0; K <= J; k++)            {                if(Cmd[i] = ='F')                {                    if(K &1) {dp[i][j][0] = max (dp[i][j][0], Dp[i-1][j-k][1]); dp[i][j][1] = max (dp[i][j][1], Dp[i-1][j-k][0]); }                    Else{dp[i][j][0] = max (dp[i][j][0], Dp[i-1][j-k][0] +1); dp[i][j][1] = max (dp[i][j][1], Dp[i-1][j-k][1] -1); }                }                Else                {                    if(K &1) {dp[i][j][0] = max (dp[i][j][0], Dp[i-1][j-k][0] +1); dp[i][j][1] = max (dp[i][j][1], Dp[i-1][j-k][1] -1); }                    Else{dp[i][j][0] = max (dp[i][j][0], Dp[i-1][j-k][1]); dp[i][j][1] = max (dp[i][j][1], Dp[i-1][j-k][0]); } }}}} printf ("%d\n", Max (dp[len][n][0], dp[len][n][1])); return 0;}
View Code

 

Codeforces 132C Logo Turtle (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.