-
Notifications
You must be signed in to change notification settings - Fork 0
/
Robo_45_and_90.java
108 lines (102 loc) · 2.83 KB
/
Robo_45_and_90.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package test;
import java.util.*;
public class trial1
{
public static void main(String[] args)
{
int x =7, y =7, increment = 0;
String pos = "4-6-SW", move = "M r m l M m R", result = "", final_dir = "";
String movement[] = move.split(" ");
String initial[] = pos.split("-");
int robo_x = Integer.parseInt(initial[0]), robo_y = Integer.parseInt(initial[1]);
HashMap<String, Integer> dir = new HashMap<>();
dir.put("N", 0);
dir.put("E", 90);
dir.put("S", 180);
dir.put("W", 270);
dir.put("NE", 45);
dir.put("SE", 135);
dir.put("SW", 225);
dir.put("NW", 315);
int border_exceed_flag = 0, degree = dir.get(initial[2]);
for(String i:movement)
{
if(i.equals("L"))
{
degree -= 90;
}
else if(i.equals("R"))
{
degree += 90;
}
else if(i.equals("l"))
{
degree -= 45;
}
else if(i.equals("r"))
{
degree += 45;
}
if(degree < 0)
{
degree = 360 + degree;
}
if(i.equals("M") || i.equals("m"))
{
if(i.equals("M"))
{
increment = 2;
}
else if(i.equals("m"))
{
increment = 1;
}
switch(degree)
{
case 0: robo_y += increment; break;
case 45: robo_x += increment; robo_y += increment; break;
case 90: robo_x += increment; break;
case 135: robo_x += increment; robo_y -= increment; break;
case 180: robo_y -= increment; break;
case 225: robo_x -= increment; robo_y -= increment; break;
case 270: robo_x -= increment; break;
case 315: robo_x -= increment; robo_y += increment; break;
}
if((robo_x > x || robo_y > y) || (robo_x < 0 || robo_y < 0))
{
switch(degree)
{
case 0: robo_y -= increment; break;
case 45: robo_x -= increment; robo_y -= increment; break;
case 90: robo_x -= increment; break;
case 135: robo_x -= increment; robo_y += increment; break;
case 180: robo_y += increment; break;
case 225: robo_x += increment; robo_y += increment; break;
case 270: robo_x += increment; break;
case 315: robo_x += increment; robo_y -= increment; break;
}
border_exceed_flag = 1;
break;
}
}
degree = degree%360;
}
for(String k : dir.keySet())
{
if(dir.get(k) == degree)
{
final_dir = k;
break;
}
}
if(border_exceed_flag == 1)
{
result = robo_x+"-"+robo_y+"-"+final_dir+"-ER";
}
else
{
result = robo_x+"-"+robo_y+"-"+final_dir;
}
System.out.println(result);
}
}