题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f1fefe34f7a3e9c08425284f06df8a22.png)
代码
class Solution {
public int robotSim(int[] commands, int[][] obstacles) {
int[][] directions = new int[][]{{0,1},{1,0},{0,-1},{-1,0}};
int direction = 0,x=0,y=0,aer = 0;
HashSet hashSet = new HashSet();
for(int[] obs : obstacles){
hashSet.add(obs[0]+","+obs[1]);
}
for(int com : commands){
if(com==-1) direction = (direction+1)%4;
else if(com==-2) direction = (direction+3)%4;
else if(com>0){
while (com -- >0){
int nextX = x+directions[direction][0];
int nextY = y+directions[direction][1];
if(!hashSet.contains(nextX+","+nextY)){
x = nextX;
y = nextY;
aer = Math.max(aer,x*x+y*y);
}else break;
}
}
}return aer;
}
}
结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/545cbf5afbf7336fc3ca21dd4be759c9.png)