Problem
Banny has just bought a new programmable robot. Eager to test his coding skills, he has placed the robot in a grid of squares with R rows (numbered 1 to R from north to south) and C columns (numbered 1 to C from west to east). The square in row r and column c is denoted (r, c).
Initially, the robot starts in the square (SR, SC). Banny will give the robot N instructions. Each instruction is one of N, S, E or W, instructing the robot to move one square north, south, east or west respectively.
If the robot moves into a square that it has been in before, the robot will continue moving in the same direction until it reaches a square that it has not been in before. Banny will never give the robot an instruction that will cause it to move out of the grid.
Can you help Banny determine which square the robot will finish in, after following the N instructions?
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing the five integers N, R, C, SR and SC, the number of instructions, the number of rows, the number of columns, the robot's starting row and starting column, respectively.
Then, another line follows containing a single string of N characters; the i-th of these characters is the i-th instruction Banny gives the robot (one of N, S, E or W, as described above).
My solution
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class WiggleWalk {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int T = Integer.parseInt(input.nextLine());
for (int ks = 1; ks <= T; ks++) {
int[] ax = Arrays.stream(input.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
String instructions = input.nextLine();
int N = ax[0], Sr = ax[3], Sc = ax[4];
ArrayList<int[]> visitedArrayList = new ArrayList<int[]>();
int[] curPos = {Sr, Sc};
visitedArrayList.add(curPos);
for (int u = 0; u < N; u++) {
go(curPos, visitedArrayList, instructions.charAt(u));
}
System.out.println("Case #" + ks + ": " + curPos[0] + " " + curPos[1]);
}
input.close();
}
static void go(int[] cur, ArrayList<int[]> vArrayList, char c) {
if (c == 'E') {
cur[0]++;
if (!(vArrayList.contains(cur))) {
vArrayList.add(cur);
} else {
go(cur, vArrayList, c);
}
}
if (c == 'W') {
cur[0]--;
if (!(vArrayList.contains(cur))) {
vArrayList.add(cur);
} else {
go(cur, vArrayList, c);
}
}
if (c == 'N') {
cur[1]++;
if (!(vArrayList.contains(cur))) {
vArrayList.add(cur);
} else {
go(cur, vArrayList, c);
}
}
if (c == 'S') {
cur[1]--;
if (!(vArrayList.contains(cur))) {
vArrayList.add(cur);
} else {
go(cur, vArrayList, c);
}
}
}
}
Logic
First increment current position. If new cell is not already visited, visit it (by adding current position to the list of visited cells). If it is already visited, recurse. I don't understand why it is recursing infinitely. At some point, it will visit an unvisited cell, right? Which means it won't recurse
Test input
3
5 3 6 2 3
EEWNS
Error
Exception in thread "main" java.lang.StackOverflowError
at java.base/java.util.ArrayList.indexOf(ArrayList.java:284)
at java.base/java.util.ArrayList.contains(ArrayList.java:273)
at WiggleWalk.go(WiggleWalk.java:27)
then at WiggleWalk.go(WiggleWalk.java:30)
many many times.
line 27 is if (!(vArrayList.contains(cur))) {
line 30 is go(cur, vArrayList, c);