题目874.模拟行走机器人
-
用x表示机器人横坐标,y表示纵坐标,commands数组,
-
-1之前若y+,则x+,x+转为y-,y-转为x-,x-转为y+
-
-2若之前y+则x-,x- y- ,y- x+ ,x+ y+
-
其他情况则根据之前的方向加x,若加完大于等于障碍物,则到障碍物-1位置,执行转向指令。
-
代码还没来得及加注释,不过挺好看懂的其实,思路清晰
-
今天debug了一晚上,主要是两点问题
-
今天debug了一晚上,主要是两点问题
- 路径问题,之前的方向指针没有if else结构,导致方向不正确。通过不加障碍,逐步排查出错步骤,分析,发现。
- 避障问题,说来真有意思,想了一晚上也没想出来的问题,我刚躺床上拿出ipad画了画就发现了,如果机器人当前位置加上指令移动步数等于障碍坐标怎么办,应该臂章,所以在判断表达式中应加入等号。
-
这就是我的代码的两大细节问题,修正后,成功通过,用时较长,但消耗内存少。
-
启发:编写代码,要严谨。结合图形分析问题会更清晰严谨。注重细节吧,有时候细节就是bug所在之处。
-
List<Integer> list = new ArrayList<>(); int x = 0, y = 0; int toward = 0; Boolean flag = false; for (int i = 0; i < commands.length; i++) { flag = false; if (commands[i] == -1) { if (toward == 3) { toward -= 3; }else toward += 1; } else if (commands[i] == -2) { if (toward == -3) { toward += 3; }else toward -= 1; } else { if (toward == 0) { int Min = 0; for (int k = 0; k < obstacles.length; k++) { if (x == obstacles[k][0]) { if ((y >= (obstacles[k][1] - commands[i])) && y < obstacles[k][1]) { if (obstacles[k][1] < Min || Min == 0) { Min = obstacles[k][1]; } flag = true; } } } if (!flag) { y += commands[i]; } else y = Min - 1; } if (toward == 3 || toward == -1) { int Max = 0; for (int k = 0; k < obstacles.length; k++) { if (y == obstacles[k][1]) { if ((obstacles[k][0] >= (x - commands[i])) && x > obstacles[k][0]) { if (obstacles[k][0] > Max || Max == 0) Max = obstacles[k][0]; flag = true; } } } if (!flag) { x -= commands[i]; } else x = Max + 1; } if (toward == -3 || toward == 1) { int Min = 0; for (int k = 0; k < obstacles.length; k++) { if (y == obstacles[k][1]) { if ((obstacles[k][0] <= (x + commands[i])) && x < obstacles[k][0]) { if (obstacles[k][0] < Min || Min == 0) Min = obstacles[k][0]; flag = true; } } } if (!flag) { x += commands[i]; } else x = Min - 1; } if (toward == -2 || toward == 2) { int Max = 0; for (int k = 0; k < obstacles.length; k++) { if (x == obstacles[k][0]) { if ((obstacles[k][1] >= (y - commands[i])) && y > obstacles[k][1]) { if (obstacles[k][1] > Max || Max == 0) Max = obstacles[k][1]; flag = true; } } } if (!flag) { y -= commands[i]; } else y = Max + 1; } //判断故障位置有问题,应判断所有故障 /* for(int k=0;k<obstacles.length;k++){ if(x==obstacles[k][0]){ if((y<(obstacles[k][1]+commands[i]))&&toward==0&&y>obstacles[k][1]) { y=obstacles[k][1]-1; break; } if((obstacles[k][1]<(y+commands[i]))&&(toward==-2||toward==2)&&y<obstacles[k][1]) { y = obstacles[k][1] + 1; break; } } if(y==obstacles[k][1]){ if((obstacles[k][0]<(x+commands[i]))&&(toward==3||toward==-1)&&x<obstacles[k][0]){ x=obstacles[k][0]+1; break; } if((obstacles[k][0]>(x-commands[i]))&&(toward==1||toward==-3)&&x>obstacles[k][0]) { x=obstacles[k][0]-1; break; } } }*/ /* for(int k=0;k<obstacles[0].length;k++){ if(y==obstacles[1][k]){ if(obstacles[0][k]>(x-commands[i])&&(toward==-3||toward==1)&&x>obstacles[0][k]) x=obstacles[0][k]-1; if(obstacles[0][k]<(x+commands[i])&&(toward==-2||toward==2)&&x<obstacles[1][k]) x=obstacles[0][k]+1; } }*/ } Integer nit = new Integer(x * x + y * y); list.add(nit); } Iterator iterator=list.iterator(); int b=0; while(iterator.hasNext()){ Integer O=(Integer)iterator.next(); if(O.intValue()>b||b==0){ b=O.intValue(); } } return b;