CF 1481A Space Navigation 题解
题目传送门
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces.
Space can be represented as the XY plane. You are starting at point (0,0), and Planetforces is located in point (px,py).
The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x,y) and current order is si:
if si=U, you move to (x,y+1);
if si=D, you move to (x,y−1);
if si=R, you move to (x+1,y);
if si=L, you move to (x−1,y).
Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions.
Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders?
Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.
Each test case consists of two lines. The first line in each test case contains two integers px and py (−105≤px,py≤105; (px,py)≠(0,0)) — the coordinates of Planetforces (px,py).
The second line contains the string s (1≤|s|≤105: |s| is the length of string s) — the list of orders.
It is guaranteed that the sum of |s| over all test cases does not exceed 105.
Output
For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower).
Example
input
6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR
output
YES YES YES NO YES NO
Note
In the first case, you don't need to modify s, since the given s will bring you to Planetforces.
In the second case, you can delete orders s2, s3, s4, s6, s7 and s8, so s becomes equal to "UR".
In the third test case, you have to delete order s9, otherwise, you won't finish in the position of Planetforces.
题目解析
我们发现,我们可以任意删除操作,所以我们只要留下朝终点的方向的操作即可,然后我们就可以判断个数是否能够到达终点即可。
代码:
#include<cstdio> #include<cstring> #include<iostream> #define maxn 1000039 using namespace std; //#define debug typedef int Type; inline Type read(){ Type sum=0; int flag=0; char c=getchar(); while((c<'0'||c>'9')&&c!='-') c=getchar(); if(c=='-') c=getchar(),flag=1; while('0'<=c&&c<='9'){ sum=(sum<<1)+(sum<<3)+(c^48); c=getchar(); } if(flag) return -sum; return sum; } int T; int canx,cany; int x,y; int l,r,u,d,n; char s[maxn]; int main(){ cin>>T; while(T--){ cin>>x>>y; l=r=u=d=0; cin>>s; n=strlen(s); for(int i=0;i<n;i++) if(s[i]=='L') l++; else if(s[i]=='R') r++; else if(s[i]=='U') u++; else d++; if(y>=0){ if(y<=u) canx=1; else canx=0; } if(y<0){ if(-y<=d) canx=1; else canx=0; } if(x>=0){ if(x<=r) cany=1; else cany=0; } if(x<0){ if(-x<=l) cany=1; else cany=0; } if(canx==1&&cany==1) printf("YES\n"); else printf("NO\n"); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具