class : defrobot(self, command: str, obstacles: List[List[int]], x: int, y: int) -> bool: if x == 0and y == 0: returnTrue x0 = y0 = 0 while(True): for ch in command: if ch == 'U': y0 += 1 else: x0 += 1 for obs in obstacles: if obs[0] > x0 or obs[1] > y0: continue if obs == [x0, y0]: returnFalse # 抵达终点 if x0 == x and y0 == y: returnTrue
class : defrobot(self, command: str, obstacles: List[List[int]], x: int, y: int) -> bool: if x == 0and y == 0: returnTrue ori, oripos = [0, 0], [] for ch in command: if ch == 'R': ori[0] += 1 else: ori[1] += 1 oripos.append(ori[:]) divpos = oripos[-1] # 判断终点是否在行走路径上 for pos in oripos: if (pos[0] == x and pos[1] == y) or (pos[1] != y and (x-pos[0])/(y-pos[1大专栏 机器人冒险span>]) == divpos[0]/divpos[1]): break else: returnFalse # 判断障碍物是否在行走路径上 for obs in obstacles: # 障碍物超出终点 if obs[0] > x or obs[1] > y: continue for pos in oripos: # 判断某个障碍物是否在行走路径上 if (pos[0] == obs[0] and pos[1] == obs[1]) or (pos[1] != obs[1] and (obs[0]-pos[0])/(obs[1]-pos[1]) == divpos[0]/divpos[1]): returnFalse returnTrue