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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| class Robot {
int width, height; int loc; boolean moved; String[] dir = new String[]{"East", "North", "West", "South"};
public Robot(int width, int height) { this.width = width; this.height = height; }
public void step(int num) { moved = true; loc += num; loc %= 2 * (width - 1) + 2 * (height - 1); }
public int[] getPos() { int[] pos = move(); int x = pos[0], y = pos[1]; return new int[]{pos[0], pos[1]}; }
public String getDir() { int[] pos = move(); int x = pos[0], y = pos[1], d = pos[2]; if (x == 0 && y == 0) return moved ? dir[3] : dir[0]; return dir[d]; }
private int[] move() { if (loc <= width - 1) { return new int[]{loc, 0, 0}; } else if (loc <= (width - 1) + (height - 1)) { return new int[]{width - 1, loc - (width - 1), 1}; } else if (loc <= 2 * (width - 1) + (height - 1)) { return new int[]{(width - 1) - (loc - ((width - 1) + (height - 1))), height - 1, 2}; } else { return new int[]{0, (height - 1) - (loc - (2 * (width - 1) + height - 1)), 3}; } } }
|