FT Robot(AtCoder-3726)
Problem Description
A robot is put at the origin in a two-dimensional plane. Initially, the robot is facing in the positive x-axis direction.
This robot will be given an instruction sequence s. s consists of the following two kinds of letters, and will be executed in order from front to back.
F : Move in the current direction by distance 1.
T : Turn 90 degrees, either clockwise or counterclockwise.
The objective of the robot is to be at coordinates (x,y) after all the instructions are executed. Determine whether this objective is achievable.Constraints
- s consists of
F
andT
.- 1≤|s|≤8 000
- x and y are integers.
- |x|,|y|≤|s|
Input
Input is given from Standard Input in the following format:
s
x yOutput
If the objective is achievable, print Yes; if it is not, print No.
Example
Sample Input 1
FTFFTFFF
4 2Sample Output 1
Yes
The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.Sample Input 2
FTFFTFFF
-2 -2Sample Output 2
Yes
The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.Sample Input 3
FF
1 0Sample Output 3
No
Sample Input 4
TF
1 0Sample Output 4
No
Sample Input 5
FFTTFF
0 0Sample Output 5
Yes
The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.Sample Input 6
TTTT
1 0Sample Output 6
No
题意:有一个二维坐标平面,初始时有个机器人在 (0,0) 点,现在给出一串字符序列代表一系列指令与一个坐标,F 代表沿当前方向移动 1 格,T 代表顺时针或逆时针转 90 度,问在执行完指令后能否到达坐标
思路:
根据给出的指令,分别统计 x、y 可达的位置,将可达的点标记出来,如果最后 x、y 位置都可达,那么就输出 Yes,否则输出 No
由于 x、y 的范围从 -8000 到 8000,因此要开的空间为 2*8000,在计算时相应进行偏移
利用 bitset,开一个二维的空间,第一维记录 x 可达的位置,第二维记录 y 可达的位置,在初始时统计有多少连续的 F,即为第一个可达的 x 点,之后从第一个可达的位置开始,看指令序列中有多少个连续的 TFFFFF,根据连续的 F 的个数,对 x、y 的进行相应的位移
最后看 x、y 是否可达即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 8000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int x,y;
char str[N];
bitset<N*2> bs[2];
int main(){
scanf("%s",str);
scanf("%d%d",&x,&y);
int i=0;
while(str[i]=='F')
i++;
int nx=i+N,ny=N;
bs[0][nx]=1;//x可达点
bs[1][ny]=1;//y可达点
int num=0;
int pos=0;//0为在x轴移动,1为在y轴移动,相应的对应顺时针、逆时针转动后F运行的方向
while(i<=strlen(str)){
if(str[i]=='F')
num++;
else{
bs[pos]=(bs[pos]<<num) | (bs[pos]>>num);//根据F连续的个数记录进行位移
pos=!pos;//进行转动
num=0;
}
i++;
}
nx=x+N,ny=y+N;
if(bs[0][nx] & bs[1][ny])//判断是否可达
printf("Yes\n");
else
printf("No\n");
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】