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×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×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).
image

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; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16266370.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(42)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示