class Solution {
public:introbotSim(vector<int>& commands, vector<vector<int>>& obstacles){int dirs[4][2]={{-1,0},{0,1},{1,0},{0,-1}};int sx =0, sy =0, res =0, d =1;
set<pair<int,int>> mp;for(int i =0; i < obstacles.size();++i){
pair<int,int>t(obstacles[i][0], obstacles[i][1]);
mp.insert(t);}for(int c : commands){if(c <0){
d += c ==-1?1:-1;
d %=4;if(d <0){
d +=4;}}else{for(int i =0; i < c;++i){int nx = sx + dirs[d][0];int ny = sy + dirs[d][1];
pair<int,int>t(nx, ny);if(mp.count(t)){break;}
res =max(res, nx * nx + ny * ny);
sx = nx;
sy = ny;}}}return res;}};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Python
class Solution:
def robotSim(self, commands: List[int], obstacles: List[List[int]])->int:
dirs =[[-1,0],[0,1],[1,0],[0,-1]]
sx, sy =0,0
d =1
res =0
mp =set([tuple(i)for i in obstacles])for c in commands:if c <0:
d +=1if c ==-1else-1
d %=4else:for i in range(c):iftuple([sx + dirs[d][0], sy + dirs[d][1]]) in mp:breakelse:
sx += dirs[d][0]
sy += dirs[d][1]
res =max(res, sx*sx + sy * sy)return res