C - Wandering Robot

Description

DreamGrid creates a programmable robot to explore an infinite two-dimension plane. The robot has a basic instruction sequence a1,a2,…ana1,a2,…an

and a “repeating parameter” kk

, which together form the full instruction sequence s1,s2,…,sn,sn+1,…,snks1,s2,…,sn,sn+1,…,snk

and control the robot.There are 4 types of valid instructions in total, which are ‘U’ (up), ‘D’ (down), ‘L’ (left) and ‘R’ (right). Assuming that the robot is currently at (x,y)(x,y)

, the instructions control the robot in the way below: U: Moves the robot to (x,y+1)(x,y+1)

.D: Moves the robot to (x,y−1)(x,y−1)

.L: Moves the robot to (x−1,y)(x−1,y)

.R: Moves the robot to (x+1,y)(x+1,y)

.The full instruction sequence can be derived from the following equations {si=aisi=si−nif 1≤i≤notherwise{si=aiif 1≤i≤nsi=si−notherwise

The robot is initially at (0,0)(0,0)

and executes the instructions in the full instruction sequence one by one. To estimate the exploration procedure, DreamGrid would like to calculate the largest Manhattan distance between the robot and the start point (0,0)(0,0)

during the execution of the nknk

instructions.Recall that the Manhattan distance between (x1,y1)(x1,y1)

and (x2,y2)(x2,y2)

is defined as |x1−x2|+|y1−y2|

Input

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:
The first line contains two integers nn and kk (1≤n≤105,1≤k≤1091≤n≤105,1≤k≤109), indicating the length of the basic instruction sequence and the repeating parameter.
The second line contains a string A=a1a2…anA=a1a2…an (|A|=n|A|=n, ai∈{‘L’,‘R’,‘U’,‘D’}ai∈{‘L’,‘R’,‘U’,‘D’}), where aiai indicates the ii-th instruction in the basic instruction sequence.
It’s guaranteed that the sum of |A||A| of all test cases will not exceed 2×1062×106.

Output

For each test case output one line containing one integer indicating the answer.

Sample Input

2
3 3
RUL
1 1000000000
D

Sample Output

4
1000000000

Hint

For the first sample test case, the final instruction sequence is “RULRULRUL” and the route of the robot is (0, 0) - (1, 0) - (1, 1) - (0, 1) - (1, 1) - (1, 2) - (0, 2) - (1, 2) - (1, 3) - (0, 3). It’s obvious that the farthest point on the route is (1, 3) and the answer is 4.

解题思路

可能唯一的坑点就是最远处可能在第一次

AC代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll t,n,m;
    string s;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>s;
        ll x=0,y=0,ans=0;
        for(int i=0; i<s.size(); ++i)
        {
            if(s[i]=='U')
                ++y;
            else if(s[i]=='D')
                --y;
            else if(s[i]=='L')
                --x;
            else if(s[i]=='R')
                ++x;
            if(ans<abs(x)+abs(y))
                ans=abs(x)+abs(y);
        }
        x*=m-1,y*=m-1;
        for(int i=0; i<s.size(); ++i)
        {
            if(s[i]=='U')
                ++y;
            else if(s[i]=='D')
                --y;
            else if(s[i]=='L')
                --x;
            else if(s[i]=='R')
                ++x;
            if(ans<abs(x)+abs(y))
                ans=abs(x)+abs(y);
        }
        cout<<ans<<endl;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值