给你一个在 XY 平面上的 width x height 的网格图,左下角 的格子为 (0, 0) ,右上角 的格子为 (width - 1, height - 1) 。网格图中相邻格子为四个基本方向之一("North","East","South" 和 "West")。一个机器人 初始 在格子 (0, 0) ,方向为 "East" 。
机器人可以根据指令移动指定的 步数 。每一步,它可以执行以下操作。
- 沿着当前方向尝试 往前一步 。
- 如果机器人下一步将到达的格子 超出了边界 ,机器人会 逆时针 转 90 度,然后再尝试往前一步。
如果机器人完成了指令要求的移动步数,它将停止移动并等待下一个指令。
请你实现 Robot 类:
Robot(int width, int height)初始化一个width x height的网格图,机器人初始在(0, 0),方向朝"East"。void move(int num)给机器人下达前进num步的指令。int[] getPos()返回机器人当前所处的格子位置,用一个长度为 2 的数组[x, y]表示。String getDir()返回当前机器人的朝向,为"North","East","South"或者"West"。
示例 1:
输入:
["Robot", "move", "move", "getPos", "getDir", "move", "move", "move", "getPos", "getDir"]
[[6, 3], [2], [2], [], [], [2], [1], [4], [], []]
输出:
[null, null, null, [4, 0], "East", null, null, null, [1, 2], "West"]
解释:
Robot robot = new Robot(6, 3); // 初始化网格图,机器人在 (0, 0) ,朝东。
robot.move(2); // 机器人朝东移动 2 步,到达 (2, 0) ,并朝东。
robot.move(2); // 机器人朝东移动 2 步,到达 (4, 0) ,并朝东。
robot.getPos(); // 返回 [4, 0]
robot.getDir(); // 返回 "East"
robot.move(2); // 朝东移动 1 步到达 (5, 0) ,并朝东。
// 下一步继续往东移动将出界,所以逆时针转变方向朝北。
// 然后,往北移动 1 步到达 (5, 1) ,并朝北。
robot.move(1); // 朝北移动 1 步到达 (5, 2) ,并朝 北 (不是朝西)。
robot.move(4); // 下一步继续往北移动将出界,所以逆时针转变方向朝西。
// 然后,移动 4 步到 (1, 2) ,并朝西。
robot.getPos(); // 返回 [1, 2]
robot.getDir(); // 返回 "West"
提示:
2 <= width, height <= 1001 <= num <= 105move,getPos和getDir总共 调用次数不超过104次。
Related Topics
JAVA模拟
模拟得有点费劲
- 调用
move()方法的时候并不去实际模拟走动,而是增加步数并取模 - 调用
getPos()和getDir()的时候才去模拟走动 - 初始
direction定义为SOUTH,后面取模为0的话不用另算 - 但是初始定为
SOUTH初始查询方向的时候就不对了,所以再加一个moved判断,只要执行过move()就返回真实的direction,没执行过moved的话,就返回向东
可能还是数学计算的方法简单点吧,
class Robot {
int[][] matrix;
private static String NORTH = "North";
private static String EAST = "East";
private static String SOUTH = "South";
private static String WEST = "West";
private int[] position;
private static String[] dirArr;
{
dirArr = new String[]{ EAST, NORTH, WEST, SOUTH };
}
private int direction = 3;
private int perimeter = 0;
private int count = 0;
boolean moved = false;
public Robot(int width, int height) {
matrix = new int[width][height];
position = new int[2];
perimeter = (width * 2 + height * 2) - 4;
}
public void move(int num) {
count += num;
count %= perimeter;
moved = true;
}
/**
*
* 西 ↑
* 南 北
* ← →
* 东 ↓
*
*/
private void goNext(){
int[] next;
switch (direction){
case 0:
next = new int[]{position[0]+1,position[1]};
break;
case 1:
next = new int[]{position[0],position[1]+1};
break;
case 2:
next = new int[]{position[0]-1,position[1]};
break;
case 3:
next = new int[]{position[0],position[1]-1};
break;
default:
return;
}
if (next[0] >= matrix.length || next[1] >= matrix[0].length || next[0]<0 ||next[1] <0){
direction = (direction+1)%4;
goNext();
return ;
}
position = next;
}
private void __move(){
while (count>0){
goNext();
count--;
}
}
public int[] getPos() {
__move();
return position;
}
public String getDir() {
__move();
return moved?dirArr[direction]:dirArr[0];
}
}

发表评论