0874.cpp (952B)
1 class Solution { 2 public: 3 int robotSim(const vector<int> &commands, vector<vector<int>> &obstacles) const { 4 unordered_map<int, unordered_set<int>> us; 5 6 for (const auto &obstacle : obstacles) { 7 us[obstacle[0]].emplace(obstacle[1]); 8 } 9 10 int res = 0; 11 int x = 0, y = 0, dir = 0; 12 static const int offset[] = {0, 1, 0, -1, 0}; 13 for (const auto &command : commands) { 14 switch (command) { 15 case -2: dir = (dir + 3) % 4; break; 16 case -1: dir = (dir + 5) % 4; break; 17 default: 18 for (int i = 0; i < command; i++) { 19 const int nx = x + offset[dir]; 20 const int ny = y + offset[dir + 1]; 21 22 if (us[nx].count(ny)) break; 23 24 x = nx, y = ny; 25 res = max(res, x * x + y * y); 26 } 27 } 28 } 29 30 return res; 31 } 32 };