ZOJ - 4115:Wandering Robot

DreamGrid创建可编程机器人探索二维平面,机器人有基本指令序列和重复参数构成完整指令序列。指令有'U'、'D'、'L'、'R'四种,控制机器人移动。初始位置为(0, 0),需计算执行指令过程中与起点的最大曼哈顿距离,还给出输入输出格式及示例。

DreamGrid creates a programmable robot to explore an infinite two-dimension plane. The robot has a basic instruction sequence  and a "repeating parameter" , which together form the full instruction sequence  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 , the instructions control the robot in the way below:

  • U: Moves the robot to .
  • D: Moves the robot to .
  • L: Moves the robot to .
  • R: Moves the robot to .

 

The full instruction sequence can be derived from the following equations

The robot is initially at  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  during the execution of the  instructions.

Recall that the Manhattan distance between  and  is defined as .

Input

There are multiple test cases. The first line of the input contains an integer indicating the number of test cases. For each test case:

The first line contains two integers  and  (), indicating the length of the basic instruction sequence and the repeating parameter.

The second line contains a string  (, ), where  indicates the -th instruction in the basic instruction sequence.

It's guaranteed that the sum of  of all test cases will not exceed .

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.

 

#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <cmath>

using namespace std;

int n;
long long k;

struct dian{
    long long x,y;
};

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %lld",&n,&k);
        dian one;
        one.x = 0;one.y = 0;
        vector<dian> site;
        char s[2];
        
        for(int i = 0;i<n;i++)//走第一圈
        {
            scanf("%1s",s);
            if(!strcmp(s,"U"))
                one.y += 1;
            if(!strcmp(s,"D"))
                one.y -= 1;
            if(!strcmp(s,"L"))
                one.x -= 1;
            if(!strcmp(s,"R"))
                one.x += 1;
            site.push_back(one);
        }

        long long px = one.x * (k-1);
        long long py = one.y * (k-1);

        for(int i = 0;i<n;i++)//走最后一圈
        {
            dian last;
            last.x = px+site[i].x;
            last.y = py+site[i].y;
            site.push_back(last);
        }

        long long maxn = -1000;
        for(int i = 0;i<2*n;i++)
        {
            if(abs(site[i].x) + abs(site[i].y) > maxn)
            {
                maxn = abs(site[i].x) + abs(site[i].y);
            }
        }
        printf("%lld\n",maxn);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱事顺利、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值