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

2023-11-10 22:00

本文主要是介绍Wandering Robot ( ZOJ 4115 ) (第十届ACM山东省省赛 - C题 ),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

欢迎访问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题 )的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/385550

相关文章

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

数论ZOJ 2562

题意:给定一个数N,求小于等于N的所有数当中,约数最多的一个数,如果存在多个这样的数,输出其中最大的一个。 分析:反素数定义:对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4.如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x为反素数。 性质一:一个反素数的质因子必然是从2开始连续的质数。 性质二:p=2^t1*3^t2*5^t3*7

zoj 1721 判断2条线段(完全)相交

给出起点,终点,与一些障碍线段。 求起点到终点的最短路。 枚举2点的距离,然后最短路。 2点可达条件:没有线段与这2点所构成的线段(完全)相交。 const double eps = 1e-8 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;

zoj 4624

题目分析:有两排灯,每排n个,每个灯亮的概率为p,每个灯之间互不影响,亮了的灯不再灭,问两排中,每排有大于等于m个灯亮的概率。 设dp[ i ][ j ]为第一排亮了i个灯,第二排亮了j个灯,距离目标状态的期望天数。显然 i >= m ,j >= m时 , dp[ i ][ j ] = 0 。 状态转移 : 第一排亮了a个灯,a 在[ 0 , n - i] 之间,第二排亮了b个灯 , b 在

zoj 3228 ac自动机

给出一个字符串和若干个单词,问这些单词在字符串里面出现了多少次。单词前面为0表示这个单词可重叠出现,1为不可重叠出现。 Sample Input ab 2 0 ab 1 ab abababac 2 0 aba 1 aba abcdefghijklmnopqrstuvwxyz 3 0 abc 1 def 1 jmn Sample Output Case 1 1 1 Case 2

ZOJ Monthly, August 2014小记

最近太忙太忙,只能抽时间写几道简单题。不过我倒是明白要想水平提高不看题解是最好的了。 A  我只能死找规律了,无法证明 int a[50002][2] ;vector< vector<int> > gmax , gmin ;int main(){int n , i , j , k , cmax , cmin ;while(cin>>n){/* g

【转载】ACM感悟

今天看了一篇我们学校前辈的ACM的感悟,觉得写的十分有道理,这里转载,文章还会不断的改进和更新。 原文链接:http://www.cnblogs.com/Chierush/p/3760870.html?ADUIN=1339764596&ADSESSION=1401536826&ADTAG=CLIENT.QQ.5329_.0&ADPUBNO=26349 声明:本文是写给弱校ACM新手的一点

我们依旧在追梦的路上-山东省第六届ACM比赛总结

这场比赛从结果而言达到了预期(金牌),从过程而言和我的预期相差甚远(打的太乱,个人发挥很差),还好关键时刻队友抗住压力,负责后果真的不堪设想。 热身赛 热身赛纯粹测机器的,先把A,B,C草草水过(A题小写x打成大写的也是醉了),我和老高开始各种测机器,long long不出所料是lld的,试了一下除0和数组越界的re问题,发现没有re,只有wa(甚至数组越界还AC了),至于栈深的话也没过多追

ZOJ 3324 Machine(线段树区间合并)

这道题网上很多代码是错误的,由于后台数据水,他们可以AC。 比如这组数据 10 3 p 0 9 r 0 5 r 6 9 输出应该是 0 1 1 所以有的人直接记录该区间是否被覆盖过的方法是错误的 正确方法应该是记录这段区间的最小高度(就是最接近初始位置的高度),和最小高度对应的最长左区间和右区间 开一个sum记录这段区间最小高度的块数,min_v 记录该区间最小高度 cover

ACM东北地区程序设计大赛

不得不说随着参赛级别的提高,题目真的是越来越难啊,不过队长真是给力啊,在我们三个共同努力之下拿下了地区赛三等奖,哈哈我们可是大一唯一一只获奖队,终于在这次比赛打败了田大神。。。大神是失手了,俺和他差距还是挺大的。。。队友陈彤马上要去服兵役了,他说这是我们送给他最好的离别礼物,希望那家伙在部队好好干,以后谁干揍我!!!东北地区赛结束后,今年已经估计没机会参加亚洲区比赛了,赶紧补高数和线数啊!!别挂了