Skip to content

Commit 6179c50

Browse files
authored
Create Wigglewalk.java
This question was asked in Google Kickstart Sample -> I/P: 3 //No. of test cases 5 3 6 2 3 EEWNS 4 3 3 1 1 SESE 11 5 8 3 4 NEESSWWNESE O/P: Case #1: 3 2 Case #2: 3 3 Case #3: 3 7
1 parent 3326c4e commit 6179c50

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

GoogleKickstart/Wigglewalk.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
*/

0 commit comments

Comments
 (0)