欢迎访问https://blog.csdn.net/lxt_Lucia~~

宇宙第一小仙女\(^o^)/~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~

--------------------------------我只是一条可爱哒分界线-------------------------------

一、问题:

Description

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

  • U: Moves the robot to ( x, y+1 ) .
  • D: Moves the robot to ( x, y-1 ).
  • L: Moves the robot to  ( x-1, y ).
  • R: Moves the robot to ( x+1, y ).

The full instruction sequence can be derived from the following equations:

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

Recall that the Manhattan distance between (x1, y1) and (x2, y2) is defined as | x1-x2 | + | y1-y2 |.

Input

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

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

The second line contains a string  where a [ i ] indicates the i-th instruction in the basic instruction sequence.

It's guaranteed that the sum of |A| 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.

二、题意:

有一个机器人,会根据输入的命令走路,U代表向上,D代表向下,L代表向左,R代表向右,求:按给出的命令执行 k 遍的过程中,到达原点最远的距离是多少。两点距离:(x1, y1) 和 (x2, y2)按 | x1-x2 | + | y1-y2 | 计算。

三、思路:

当时做这个题的时候思路偏了,当时想的是,求出先求出第一遍的过程中经过的最远距离,再求第二遍和第三遍的差值,即每次移动当前这一次对上一次的影响,以后也都会按照这个影响进行移动,直到 k 次结束。

当时试了几十组也没找到bug,但其实这个思路的确是错的,举个栗子( 测试样例 ):

1

13 10

URDDDDLLUUUUR

答案应该是11

假设第一轮执行得到的最大值是在第三象限(左下角),然后每次的终点都在起点上面1个单位,那么在 k 还不足以让整个过程轨迹都在 x 轴上方时,这么做是正确的,但是如果 k 足够大,使得后来的过程轨迹在 x 轴上方很远,其距离超过了第一轮(左下角)的,那么最大值应该在顶部,而不再是左下角的位置了。

正确的思路:

其实只需要考虑第一次和最后一次得到的最大值就可以了,最大值只可能在这两个时候出现。

首先,单算第一次,执行一遍,得到一个最大值,执行结束后得到第一遍的终点 ( x , y ) 。

然后,将 x 和 y 分别乘 ( k-1 ) 得到了第 k-1 次的终点坐标,即第 k 次的起点坐标。

然后,再执行一遍 ( 即第 k 遍 ) ,然后比较第一次和最后一次的最大值,即为结果。

注意:要开 long long!

四、代码:

#include <cstdio>
#include <algorithm>using namespace std;
typedef long long ll;int main()
{int T, n, k;char a[100010];while( ~scanf("%d", &T) ){while(T--){scanf("%d %d", &n, &k);scanf(" %s", a);ll x = 0, y = 0, max1 = 0;for(int i = 0; i <= n-1; i++)  //求第一遍最大值{if(a[i] == 'U')y++;else if(a[i] == 'D')y--;else if(a[i] == 'L')x--;else if(a[i] == 'R')x++;max1 = max(max1, abs(x)+abs(y));}x *= (k-1), y *= (k-1);for(int i = 0; i <= n-1; i++)   //求第k遍最大值{if(a[i] == 'U')y++;else if(a[i] == 'D')y--;else if(a[i] == 'L')x--;else if(a[i] == 'R')x++;max1 = max(max1, abs(x)+abs(y));}printf("%lld\n", max1);}}return 0;
}

--------------------------------我也是有底线的---------------------------------

宇宙第一小仙女\(^o^)/~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~

Wandering Robot ( ZOJ 4115 ) (第十届ACM山东省省赛 - C题 )相关推荐

  1. 第十届ACM山东省赛总结

    省赛结束了,排名打铁,只出了四题.前三个小时只出了一道题.M题很简单,我第一次循环做的直接超时,没想到怎么优化,然后误以为有公式,推了半个小时的公式,测试都对,提交一直wa,然后考虑k>35就直 ...

  2. 2018年第十届ACM四川省省赛题解(10 / 11)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 2018ACM四川省省赛 题目链接:https://www.oj.swust.edu.cn/probl ...

  3. 第十届蓝桥杯省赛原题及参考答案

    思路:这一题没有看懂是要挑选出成绩峰值最大的5个人,还是根据平均值选出5个人求其峰值和: 第一种情况答案是490 第二种情况没算 我考试时没有看清题意,以为是求出这几组数据的最大值,成功避开了题意,所 ...

  4. 第三十届ACM/ICPC 世界总决赛题目解析

    第三十届ACM/ICPC 世界总决赛题目解析 斯坦福大学 王颖

  5. 2019年第十届蓝桥杯 - 省赛 - C/C++研究生组 - G. 扫地机器人

    2019年第十届蓝桥杯 - 省赛 - C/C++研究生组 - G. 扫地机器人 Ideas 首先我们根据数学常识可以知道,当每个机器人清扫的范围差不多时,最好都是 N / K,花的时间应该是最少的. ...

  6. 蓝桥杯:试题 历届真题 修改数组【第十届】【省赛】【研究生组】Java实现

    代码实现 方法一: package com.jl;import java.util.Scanner;/*** 历届真题 修改数组[第十届][省赛][研究生组]* @author jinlei**/ p ...

  7. java蓝桥杯省赛第十届_2019年第十届蓝桥杯省赛-迷宫(BFS/Excel大法)

    这题用dfs搜不出来,需要使用bfs并记录路径,设置好方向顺序跑就ok 然而毕竟是暴力杯,我们的原则是代码能省就省(懒癌晚期 于是乎网上便出现了形形色色的题解,笔者综合了各路大神神乎其技的思想,总结出 ...

  8. java迷宫类编程题_第十届蓝桥杯省赛java类B组 试题 E:迷宫 (动态规划之回溯法)...

    问题描述 试题 E: 迷宫 [问题描述] 下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可 以通行的地方. 010000 000100 001001 110000 迷宫的入口为 ...

  9. “科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛) 题解

    "科林明伦杯"哈尔滨理工大学第十届程序设计竞赛(同步赛) 题解 萌新又来写题解啦 原题链接 B 减成一 题意:存在n个数,每次操作可以任选一个区间使得区间内的所有数字减一.问最少多 ...

  10. 糖果(2019第十届蓝桥杯省赛C++A组I题) 解题报告(状压dp) Apare_xzc

    糖果(2019第十届蓝桥杯省赛C++A组I题) 解题报告(状压dp) xzc 2019/4/5 试题 I: 糖果 时间限制: 1.0s 内存限制: 256.0MB 本题总分:25分 [问题描述]    ...

最新文章

  1. 转行python经验_【经验分享】转行如何自学Python并且找到工作,分享自己心得
  2. python3的3D实战-基于panda3d(2)
  3. 计算机win7分盘,win7电脑如何分盘
  4. pyqt2_官网教程
  5. vue 3D旋转木马轮播图
  6. php中arraymultisort,php 数组函数array_multisort()用法
  7. HTML autofocus
  8. 有了这个告警系统,DBA提前预警不是难题
  9. 【转】马拉松式学习与技术人员的成长性
  10. rhel5.5下安装oracle10g报libXp.so.6错误
  11. hashmap为什么8转成红黑树_看了两天HashMap源码,终于把红黑树插入平衡规则搞懂了...
  12. mysql数据库105页_MySQL数据库的常见操作
  13. deeplin显示安装空间不够_太实用了!这种冷门的显示器支架可帮了大忙了
  14. SD卡格式化咋办?数据恢复看这里!
  15. 目的路径太长如哈删除_如何清除winrar中的目标路径记录
  16. docker tomcat 多开 实例_Docker快速验证tomcat单机多实例方案
  17. 利用Tween让动画更平滑(补间动画)
  18. iOS开发之GameCenter使用
  19. IDEA和WebStorm破解教程--激活n年(随时更新)
  20. MSP430系列单片机实用C语言程序设计 张晞pdf

热门文章

  1. 【python】去除\n\r\t最佳方法
  2. 动物展受骗记&长洲岛游记
  3. 数据库调优--冷热分离
  4. LOJ #2359. 「NOIP2016」天天爱跑步(倍增+线段树合并)
  5. 前后端对称加密(AES)
  6. 智和信通SugarNMS工业交换机网管解决方案
  7. java对word各种文件类型的转换
  8. linux两台机器 配置免密
  9. teensy 制作键盘 linux,利用Arduino快速制作Teensy BadUSB
  10. 公式实现:如何计算某一天是星期几?