-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoundingRobots.java
More file actions
54 lines (51 loc) · 1.09 KB
/
Copy pathBoundingRobots.java
File metadata and controls
54 lines (51 loc) · 1.09 KB
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
import java.util.Scanner;
public class BoundingRobots {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
int w = sc.nextInt();
int l = sc.nextInt();
if (w == 0 && l == 0)
break;
int n = sc.nextInt();
int robot_x = 0;
int robot_y = 0;
int actual_x = 0;
int actual_y = 0;
for (int i = 0; i < n; i++) {
char dir = sc.next().charAt(0);
int dis = sc.nextInt();
switch (dir) {
case 'u':
robot_y += dis;
actual_y += dis;
break;
case 'd':
robot_y -= dis;
actual_y -= dis;
break;
case 'l':
robot_x -= dis;
actual_x -= dis;
break;
case 'r':
robot_x += dis;
actual_x += dis;
break;
}
if (actual_x > w-1)
actual_x = w-1;
else if (actual_x < 0)
actual_x = 0;
if (actual_y > l-1)
actual_y = l-1;
else if (actual_y < 0)
actual_y = 0;
}
System.out.printf("Robot thinks %d %d\n", robot_x, robot_y);
System.out.printf("Actually at %d %d\n", actual_x, actual_y);
System.out.println();
}
sc.close();
}
}