Cf 132c logo turtle [DP + Memory search]

Source: Internet
Author: User

A string composed of FT is given. f Represents Forward, t represents turn, and the initial direction is 1. After turning, F changes from 1 to-1 (or-1 to 1)

For example, the value of ftfffffff is 3.

Given N, you must perform n operations (Change F to T or change t to F) to find how far forward from the original place (absolute value)


You can search to solve this problem.

For each trip, we can choose

Continue in the current direction

Or change the direction.

In a rough estimation, the maximum length of a string is 100, which is 2 ^ 100,High complexity


But for the four variables

In the current direction, how far is the current process? The number of remaining operations in the first few itineraries

The maximum distance that can be taken is determined.


Then you can perform a memory-based search.



# Include <cstdio> # include <iostream> # include <algorithm> # include <cstring> # include <cmath> using namespace STD; char STR [111]; int DP [2] [222] [111] [55]; int m; int DFS (INT Dir, int POs, int I, int left) {If (! STR [I] & left) return 0; // return 0if (DP [dir] [POS + 100] [I] [left]! =-1) return DP [dir] [POS + 100] [I] [left]; If (! STR [I] &! Left) return DP [dir] [POS + 100] [I] [left] = ABS (POS ); // return posint mv = (DIR = 1 )? 1:-1; if (Left = 0) DP [dir] [POS + 100] [I] [left] = DFS (Dir, POS + mV, I + 1, left-(STR [I]! = 'F'); // otherwise, return DP [dir] [POS + 100] [I] [left] = max (DFS (Dir, pos + mV, I + 1, left-(STR [I]! = 'F'), DFS (! Dir, POs, I + 1, left-(STR [I]! = 'T');} int main () {# ifndef online_judgefreopen ("/home/raw.96/in.txt", "r", stdin); # endifmemset (DP, -1, sizeof (DP); CIN> STR> m; int ans = 0; For (INT Mm = m; MM> = 0; Mm-= 2) {ans = max (ANS, DFS (1,0, 0, MM); // cout <"A:" <ans <Endl; /// cout <mm <Endl;} cout <ans <Endl; return 0 ;}


Cf 132c logo turtle [DP + Memory search]

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.