File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ public class Wigglewalk {
3
+ public static void main (String [] args ) {
4
+ Scanner scan = new Scanner (System .in );
5
+ int n =scan .nextInt ();
6
+ for (int i = 1 ;i <=n ;i ++){
7
+ int n1 =scan .nextInt ();int r =scan .nextInt ();int c =scan .nextInt ();int sr =scan .nextInt ();int sc =scan .nextInt ();scan .nextLine ();String command ;
8
+ List <List <Integer >> lists = new ArrayList <>();
9
+ r =sr ;c =sc ;
10
+ lists .add (List .of (r , c ));
11
+ String tot_command =scan .nextLine ();
12
+ for (int k =0 ;k <n1 ;k ++){
13
+ command =Character .toString (tot_command .charAt (k ));
14
+ if (command .equals ("N" )){
15
+ r =r -1 ;
16
+ }else if (command .equals ("S" )){
17
+ r =r +1 ;
18
+ }else if (command .equals ("W" )){
19
+ c =c -1 ;
20
+ }else if (command .equals ("E" )){
21
+ c =c +1 ;
22
+ }
23
+ while (true ){
24
+ if (lists .contains (List .of (r , c ))){
25
+ if (command .equals ("N" )){
26
+ r =r -1 ;
27
+ }else if (command .equals ("S" )){
28
+ r =r +1 ;
29
+ }else if (command .equals ("W" )){
30
+ c =c -1 ;
31
+ }else if (command .equals ("E" )){
32
+ c =c +1 ;
33
+ }
34
+ }else {break ;}
35
+ }
36
+ lists .add (List .of (r , c ));
37
+
38
+ }
39
+ //lists.forEach(System.out::println);
40
+ System .out .println ("Case #" +i +": " +r +" " +c );
41
+ }scan .close ();
42
+ }
43
+ }
44
+ /*
45
+ I/P: 3
46
+ 5 3 6 2 3
47
+ EEWNS
48
+ 4 3 3 1 1
49
+ SESE
50
+ 11 5 8 3 4
51
+ NEESSWWNESE
52
+
53
+ O/P: Case #1: 3 2
54
+ Case #2: 3 3
55
+ Case #3: 3 7
56
+
57
+ */
You can’t perform that action at this time.
0 commit comments