问题翻译:

The robot is located on a checkered rectangular(直角 的) board of size n×m (n rows, m columns). The rows in the board are numbered from 1 to n from top to bottom, and the columns — from 1to m from left to right.

有一个方格板n行m列

The robot is able to move from the current cell to one of the four cells adjacent by side.

机器人在方格板上可以从当前位置移动到邻近的时光位置

The sequence of commands s executed by the robot is given. Each command is denoted by one of the symbols 'L', 'R', 'D' or 'U', and triggers the movement to left, right, down or up, respectively.

给出LRDU 指令对应向左右下上移动

The robot can start its movement in any cell. The robot executes the commands starting from the first one, strictly in the order in which they are listed in s. If the robot moves beyond the edge of the board, it falls and breaks. A command that causes the robot to break is not considered successfully executed.

从任意位置开始,依次执行指令

移动到边界,失败

The robot's task is to execute as many commands as possible without falling off the board.

移动次数多且不失败

For example, on board 3×3, if the robot starts a sequence of actions s=s="RRDLUU" ("right", "right", "down", "left", "up", "up") from the central cell, the robot will perform one command, then the next command will force him to cross the edge. If the robot starts moving from the cell (2,1)(2,1) (second row, first column) then all commands will be executed successfully and the robot will stop at the cell (1,2)(1,2) (first row, second column).

The robot starts from cell (2,1) (second row, first column). It moves right, right, down, left, up, and up. In this case it ends in the cell (1,2) (first row, second column).

Determine the cell from which the robot should start its movement in order to execute as many commands as possible.

求出从哪个方格开始移动次数最多

Input

The first line contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The next 2t2t lines contain descriptions of the test cases.

In the description of each test case, the first line contains two integers nn and mm (1≤n,m≤1061≤n,m≤106) — the height and width of the field that the robot is located on. The second line of the description is a string ss consisting solely of characters 'L', 'R', 'D' and 'U' — the sequence of commands the robot executes. The string has a length from 11 to 106106 commands.

It is guaranteed that the total length of ss over all test cases does not exceed 106106.

Output

Print tt lines, each of which contains the answer to the corresponding test case. The answer to the test case are two integers rr (1≤r≤n1≤r≤n) and cc (1≤c≤m1≤c≤m), separated by a space — the coordinates of the cell (row number and column number) from which the robot should start moving to perform as many commands as possible.

If there are several such cells, you may output any of them.

题目概述:

m*n方格给定上下左右走的顺序,走到边界外失败,求在哪个方格走走的步数最多?

思路1(朴素遍历O(N*3)):

遍历m*n,把每一个方格可以走的步数都求出来,求它们的最大值(test3 超时)

#include<iostream>
#include<vector>
#include<unordered_map>
#include<string>
using namespace std;
bool valid(pair<int, int>p,pair<int,int>b)
{if (p.first < b.first||p.second<b.second||b.first<1||b.second<1){return false;}return true;
}
pair<int, int> board(pair<int, int>p, string s)
{pair<int, int>temp;int count = 0;int maxcount = 0;pair<int, int>maxindex;for (int i = 1; i <= p.first; i++){for (int j = 1; j <= p.second; j++){count = 0;temp.first = i;temp.second = j;pair<int, int>start=temp;for (int k = 0; k < s.size(); k++){if (s[k] == 'L'){temp.second--;if (valid(p, temp)){count++;}else{cout << "坐标" << i << j << "count" << count<<endl;if (maxcount <= count){maxcount = count;maxindex = start;}break;}}else if (s[k] == 'R'){temp.second++;if (valid(p, temp)){count++;}else{cout << "坐标" << i << j << "count" << count<<endl;if (maxcount <= count){maxcount = count;maxindex = start;}break;}}else if (s[k] == 'U'){temp.first--;if (valid(p, temp)){count++;}else{cout << "坐标" << i << j << "count" << count<<endl;if (maxcount <= count){maxcount = count;maxindex = start;}break;}}else if (s[k] =='D'){temp.first++;if (valid(p, temp)){count++;}else{cout << "坐标" << i << j << "count" << count<<endl;if (maxcount <= count){maxcount = count;maxindex = start;}break;}}if (k == s.size() - 1){return start;}}}}return maxindex;}
int main()
{long long m;cin >> m;vector<string>s;string s1;vector<pair<int, int>>vec;pair<int, int>temp;int row;int line;while (m--){cin >> row;cin >> line;temp.first = row;temp.second = line;vec.push_back(temp);cin >> s1;s.push_back(s1);}for (int i = 0; i < vec.size(); i++){pair<int, int>res;res = board(vec[i], s[i]);cout << res.first << " " << res.second << endl;}
}

思路2(朴素遍历的优化(O(n*2))):(超时)

上下移动和左右移动是两个垂直方向的移动互不影响,所以只要找出行移动最多的位置和列移动最多的位置,就可以唯一确定(或者多个最大值)起始坐标。

统一方法求行和列

时间复杂度降一个数量级

#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include<string>
using namespace std;
int maxx(int i, int size, string s)
{int res = 0;int temp=0;for (int j = 0; j < s.size(); j++){if (s[j] == 'R' || s[j] == 'D'){i++;if (i >= 1 && i <= size){temp++;}else{if (res <= temp){res = temp;}break;}}else if(s[j]=='L'||s[j]=='U'){i--;if (i >= 1 && i <= size){temp++;}else{if (res <= temp){res = temp;}break;}}res = max(temp, res);}return res;
}
int MAX(int size, string s)
{int res=0;int maxindex=1;//从1遍历到size,求起始点最大值for (int i = 1; i <= size; i++){int mm = maxx(i, size, s);if (res <=mm){res = mm;maxindex = i;}}return maxindex;
}
pair<int, int> board(pair<int, int>p, string s)
{string s1;//s1只储存左右方向的移动string s2;//s2只储存上下方向的移动for (int i = 0; i < s.size(); i++){if (s[i] == 'R' || s[i] == 'L'){s1 += s[i];}else{s2 += s[i];}}int m = MAX(p.first, s2);int n = MAX(p.second, s1);pair<int, int>p1;p1.first = m;p1.second = n;return p1;
}
int main()
{long long m;cin >> m;vector<string>s;string s1;vector<pair<int, int>>vec;pair<int, int>temp;int row;int line;while (m--){cin >> row;cin >> line;temp.first = row;temp.second = line;vec.push_back(temp);cin >> s1;s.push_back(s1);}for (int i = 0; i < vec.size(); i++){pair<int, int>res;res = board(vec[i], s[i]);cout << res.first << " " << res.second << endl;}
}

官方思路:(模拟)

翻译:

设棋盘n行*m列

假设点为(r,c),

如果有指令让机器人出界,要么向左一共移动超过r个格子,要么向右一共移动超过m-c+1个格子,要么向上移动超过c个格子,要么向下移动超过n-r+1个格子

失败的思路:

记录四个方向移动的最大值

Codeforces Round #753 (Div. 3)E. Robot on the Board 1相关推荐

  1. Codeforces Round #753 (Div. 3) A-E

    打的烂的一批. 目录 A. Linear Keyboard[简单 /模拟] B. Odd Grasshopper[简单 / 找规律] C. Minimum Extraction[一般 / 排序 思维] ...

  2. Codeforces Round #753 (Div. 3) C. Minimum Extraction(最小抽离)

    题目翻译: Yelisey has an array a of n integers. 数组a中有n个整数 If a has length strictly greater than 1, then ...

  3. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  4. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  5. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  6. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

  7. Codeforces Round #712 Div.2(A ~ F) 超高质量题解(每日训练 Day.15 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #712 Div.2(A ~ F) 题解 比赛链接:https:// ...

  8. Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...

  9. Codeforces Round #700 (Div. 2) D2 Painting the Array II(最通俗易懂的贪心策略讲解)看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 整场比赛的A ~ E 6题全,全部题目超高质量题解链接: Codeforces Round #700 ...

最新文章

  1. Java魔法堂:URI、URL(含URL Protocol Handler)和URN
  2. Jmater参数说明
  3. kubernetes-Deployment部署无状态服务的原理详解(七)
  4. Java正常关闭资源的方式
  5. 动态加载Fbx模型文件
  6. 使用解码逻辑创建YOLO Core ML对象检测器(四)
  7. 查询作者名长度大于8的 sql语言_从零学会SQL-入门
  8. mongodb固定集合(Capped Collection)和大文件管理(GridFS)
  9. 去掉字符串最后一个字符
  10. 浅谈Mysql底层索引原理
  11. 批量保存打开的网页到本地
  12. enfp工具箱怎么用_小丸工具箱使用技巧详细图解,值得各位学习
  13. Adobe Flash Professional CS6安装失败问题
  14. win10绝地求生游戏崩溃怎么解决
  15. android手机通过wifi控制数码管,淫技:android无屏操作之adb操控wifi
  16. HTML之调色原理分析
  17. 基于管道过滤器风格的-KWIC
  18. Ansys Zemax / SPEOS | 3片式LCD投影仪的设计与仿真
  19. 【Linux03-基本工具之GCC】Linux下的C语言编译器
  20. 物流成本管理计算机会成本,企业物流成本管理复习资料.doc

热门文章

  1. SQL Server数据库技术大全——14讲 执行计划
  2. Vijos P1772 巧妙填数【进制+置换】
  3. B00006 函数itoa()
  4. 直线分割平面问题(数学归纳法)
  5. C/C++ —— 算符优先级的问题
  6. 实用的 Python —— os.system() 在 python 语句中执行 dos 命令
  7. 如何形象地理解 Python 中的 int 类型是任意精度这一概念?
  8. python编程入门与案例详解-清华大学出版社-图书详情-《Python编程入门与案例详解》...
  9. python自动化测试-python能够做软件的自动化测试吗?
  10. python安装后如何使用-python运行环境,python安装后如何使用