anoldvozer1710.v2
Senior Member
đây nhé, mấy bài rating k8 này muỗiPython:import numpy as np class Solution: def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int: direction = np.array([0, 1]) left = np.array([[0, -1],[1, 0]]) right = np.array([[0, 1],[-1, 0]]) location = np.array([0, 0]) res = 0 obsSet = set(tuple(o) for o in obstacles) for command in commands: if command == -2: direction = np.dot(left, direction) elif command == -1: direction = np.dot(right, direction) else: for i in range(command): nextLocation = direction + location if tuple(nextLocation) in obsSet: break else: location = nextLocation res = max(res, location[0] * location[0] + location[1] * location[1]) return res
Gạch tôi làm gìbài của fen đâu?
@anoldvozer1710.v2![]()

JavaScript:
function robotSim(commands: number[], obstacles: number[][]): number {
let res = 0, x = 0, y = 0, i = 0
const dirs = [[0,1], [1, 0], [0, -1], [-1, 0]];
const set = new Set<string>();
for (const [a,b] of obstacles) set.add(`${a},${b}`)
for (const c of commands) {
if (c === -1) i = (i + 1) % 4;
else if (c === -2) i = (i + 3) % 4;
else {
const [dx, dy] = dirs[i];
for (let j = 0; j < c; j++) {
if (set.has(`${x + dx},${y + dy}`)) break;
x = x + dx, y = y + dy
}
}
res = Math.max(res, x * x + y * y)
}
return res;
};
Sửa lần cuối:





