# 3. 机器人大冒险¶

## 1、题目描述¶

1. U: 向y轴正方向移动一格
2. R: 向x轴正方向移动一格。

输入：command = "URR", obstacles = [], x = 3, y = 2



输入：command = "URR", obstacles = [[2, 2]], x = 3, y = 2



输入：command = "URR", obstacles = [[4, 2]], x = 3, y = 2



1. 2 <= command的长度 <= 1000
2. commandU，R构成，且至少有一个U，至少有一个R
3. 0 <= x <= 1e9, 0 <= y <= 1e9
4. 0 <= obstacles的长度 <= 1000
5. obstacles[i]不为原点或者终点

## 2、解题思路¶

• 直接判断当前位置的x，y需要多少个指令的循环
• 将剩余的余数进行判断，判断是否能够恰好的到这个位置
from typing import List

from collections import Counter

class Solution:
def robot(self, command: str, obstacles: List[List[int]], x: int, y: int) -> bool:
count = Counter(command)

def judge(pos_x, pos_y):
x_count = pos_x // count["R"]
x_remainder = pos_x % count["R"]
y_count = pos_y // count["U"]
y_remainder = pos_y % count["U"]

if x_count > y_count:
x_remainder += count["R"] * (x_count - y_count)
elif x_count < y_count:
y_remainder += count["U"] * (y_count - x_count)
pos = 0
while pos < len(command) and (x_remainder > 0 or y_remainder > 0):
if command[pos] == "R":
x_remainder -= 1
else:
y_remainder -= 1
pos += 1

if x_remainder == 0 and y_remainder == 0:
return True
return False

if not judge(x, y):
return False

for m, n in obstacles:
if m <= x and n <= y:
if judge(m, n):
return False
return True