Codeforces Round #753 (Div. 3)
比赛链接
Codeforces Round #753 (Div. 3)
E. Robot on the Board 1
The robot is located on a checkered rectangular board of size \(n \times m\) ( \(n\) rows, \(m\) columns). The rows in the board are numbered from 1 to \(n\) from top to bottom, and the columns - from 1 to \(m\) from left to right.
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.
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 \times 3\), if the robot starts a sequence of actions \(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)\) (second row, first column) then all commands will be executed successfully and the robot will stop at the cell \((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.
解题思路
思维
从 \((0,0)\) 模拟机器人的动作,记录四个范围:\(minx,maxx,miny,maxy\),如果机器人走的范围超出要求的范围,则无论起点在哪这时机器人都会损坏,否则计算出此时机器人的起点 \((abs(minx)+1,abs(miny)+1)\)
- 时间复杂度:\(O(n)\)
代码
// Problem: E. Robot on the Board 1
// Contest: Codeforces - Codeforces Round #753 (Div. 3)
// URL: https://codeforces.com/contest/1607/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// %%%Skyqwq
#include <bits/stdc++.h>
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
int t,n,m;
int main()
{
help;
for(cin>>t;t;t--)
{
cin>>n>>m;
string s;
cin>>s;
int minx=0,miny=0,maxx=0,maxy=0;
int resx=1,resy=1,x=0,y=0;
for(char c:s)
{
if(c=='U')minx=min(minx,--x);
else if(c=='D')maxx=max(maxx,++x);
else if(c=='L')miny=min(miny,--y);
else
maxy=max(maxy,++y);
if(maxx-minx>=n||maxy-miny>=m)break;
resx=abs(minx)+1,resy=abs(miny)+1;
}
cout<<resx<<' '<<resy<<'\n';
}
return 0;
}