当前位置:网站首页>LeetCode刷题日记:LCP 03.机器人大冒险
LeetCode刷题日记:LCP 03.机器人大冒险
2022-08-02 01:44:00 【淡墨@~无痕】
力扣团队买了一个可编程机器人,机器人初始位置在原点(0, 0)。小伙伴事先给机器人输入一串指令command,机器人就会无限循环这条指令的步骤进行移动。指令有两种:
U: 向y轴正方向移动一格
R: 向x轴正方向移动一格。
不幸的是,在 xy 平面上还有一些障碍物,他们的坐标用obstacles表示。机器人一旦碰到障碍物就会被损毁。
给定终点坐标(x, y),返回机器人能否完好地到达终点。如果能,返回true;否则返回false。
示例 1:
输入:command = "URR", obstacles = [], x = 3, y = 2
输出:true
解释:U(0, 1) -> R(1, 1) -> R(2, 1) -> U(2, 2) -> R(3, 2)。
示例 2:
输入:command = "URR", obstacles = [[2, 2]], x = 3, y = 2
输出:false
解释:机器人在到达终点前会碰到(2, 2)的障碍物。
示例 3:
输入:command = "URR", obstacles = [[4, 2]], x = 3, y = 2
输出:true
解释:到达终点后,再碰到障碍物也不影响返回结果。
限制:
2 <= command的长度 <= 1000
command由U,R构成,且至少有一个U,至少有一个R
0 <= x <= 1e9, 0 <= y <= 1e9
0 <= obstacles的长度 <= 1000
obstacles[i]不为原点或者终点
思路:模拟(超时)
class Solution {
public boolean robot(String command, int[][] obstacles, int x, int y) {
int len = command.length();
int a = 0, b = 0;
int index = 0;
while (len != 0){
if(index == len){
index = 0;
}
char str = command.charAt(index++);
if(str == 'U'){
b ++;
}else if(str == 'R'){
a ++;
}
if(a == x && b == y){
return true;
}else if(a > x || b > y){
return false;
}
for (int i = 0; i < obstacles.length; i++) {
if(obstacles[i][0] == a && obstacles[i][1] == b){
return false;
}
}
}
return false;
}
}

优化
class Solution {
public boolean robot(String command, int[][] obstacles, int x, int y) {
int len = command.length();
int a = 0, b = 0;
// 先判断走一轮先能不能走完。
for (int i = 0; i < len; i++) {
char str = command.charAt(i);
if(str == 'U'){
b ++;
}else{
a ++;
}
}
boolean res = reach(command, x, y, a, b);
if (!res){
return false;
}
for (int[] val: obstacles) {
if(val[0] > x || val[1] > y){
continue;
}
if(reach(command, val[0], val[1], a, b)){
return false;
}
}
return true;
}
public boolean reach(String command,int tx, int ty, int x, int y){
// 看至少还需要多少遍才能到达终点。
int min = Math.min(tx/x, ty/y);
int nx = x*min, ny = y*min;
if(nx == tx && ny == ty){
return true;
}
for (int i = 0; i < command.length(); i++) {
char str = command.charAt(i);
if(str == 'U'){
ny ++;
}else{
nx ++;
}
if (nx > tx || ny > ty) return false;
if (nx == tx && ny == ty) return true;
}
return true;
}
}
边栏推荐
- 软件测试功能测试全套常见面试题【开放性思维题】面试总结4-3
- 电商库存系统的防超卖和高并发扣减方案
- Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
- Shell Beginners Final Chapter
- Pcie the inbound and outbound
- R语言使用cph函数和rcs函数构建限制性立方样条cox回归模型、使用anova函数进行方差分析通过p值确认指定连续变量和风险值HR之间是否存在非线性关系
- MySQL——增删查改操作
- 6-25 Vulnerability Exploitation - irc Backdoor Exploitation
- 记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
- The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!
猜你喜欢
随机推荐
创新项目实战之智能跟随机器人原理与代码实现
雇用WordPress开发人员:4个实用的方法
Flex布局详解
Why is on-chain governance so important, and how will Polkadot Gov 2.0 lead the development of on-chain governance?
乱七八糟的网站
typescript30-any类型
hash table
Flask gets post request parameters
Can't connect to MySQL server on 'localhost3306' (10061) Simple and clear solution
Kubernetes之本地存储
kubernetes之服务发现
6-24漏洞利用-vnc密码破解
Oracle data to mysql FlinkSQL CDC to achieve synchronization
Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided
百度、百图生科 | HelixFold-Single: 使用蛋白质语言模型作为替代进行无MSA蛋白质结构预测
【Brush the title】Family robbery
力扣 1374. 生成每种字符都是奇数个的字符串
Use flex-wrap to wrap lines in flex layout
Kubernetes — Flannel
密码学的基础:X.690和对应的BER CER DER编码








