C - Wandering Robot

比眉伴天荒 2022-01-30 08:07 208阅读 0赞

在这里插入图片描述

在这里插入图片描述
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分别表示向上走,向下走,向左走,向右走。
给你一串UDLRDU…(指定方向图),让你循环k次,问你这期间距离原点最远是多少。

思路,先让它循环一次,找出最大值,然后找出他第一次循环完的那个点,平移(n-1)次,再以平移后的点为原点,在循环一次操作,找出一个最大值, 距离原点最远的值必定出现在第一次循环和最后一次循环。

AC代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<queue>
  4. #include<map>
  5. #include<cmath>
  6. using namespace std;
  7. typedef long long ll;
  8. int main()
  9. {
  10. ll t;
  11. cin>>t;
  12. while(t--)
  13. {
  14. ll n,m;
  15. string s;
  16. cin>>n>>m;
  17. cin>>s;
  18. ll mmax=0;
  19. ll x=0,y=0;
  20. for(int i=0;i<s.length();i++)
  21. {
  22. if(s[i]=='U')
  23. y++;
  24. else if(s[i]=='D')
  25. y--;
  26. else if(s[i]=='L')
  27. x--;
  28. else if(s[i]=='R')
  29. x++;
  30. mmax=max(mmax,abs(x)+abs(y));
  31. }
  32. x*=(m-1);
  33. y*=(m-1);
  34. for(int i=0;i<s.length();i++)
  35. {
  36. if(s[i]=='U')
  37. y++;
  38. else if(s[i]=='D')
  39. y--;
  40. else if(s[i]=='L')
  41. x--;
  42. else if(s[i]=='R')
  43. x++;
  44. mmax=max(mmax,abs(x)+abs(y));
  45. }
  46. cout<<mmax<<endl;
  47. }
  48. return 0;
  49. }

发表评论

表情:
评论列表 (有 0 条评论,208人围观)

还没有评论,来说两句吧...

相关阅读

    相关 robots

    robots是网站跟爬虫间的协议,用简单直接的txt格式文本方式告诉对应的爬虫被允许的权限,也就是说robots.txt是搜索引擎中访问网站的时候要查看的第一个文件。当一个搜索

    相关 Robot Motion

    链接:http://poj.org/problem?id=1573 Problem Description: 机器人已经被编程以遵循其路径中的指示。机器人要移动的下一个方向

    相关 robots协议

              Robots协议(也称为爬虫协议、机器人协议等)的全称是“网络爬虫排除标准”(Robots Exclusion Protocol),网站通过Robots协议