0

Link to problem

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);

p00f
  • 9
  • 1
  • 3
  • 1
    The array list holds a reference to the array. If you change sonething in the array, it will also update it in the entry in the `ArrayList`. Bacause of that, the `else` branch is executed and you execute the the method recursively without changing the reference. You can fix this by not adding the array to the `ArrayList` but a copy of the array. – dan1st May 15 '20 at 14:44
  • Does this answer your question? [What causes a java.lang.StackOverflowError](https://stackoverflow.com/questions/3197708/what-causes-a-java-lang-stackoverflowerror) – Zoe May 17 '20 at 11:59
  • No, I knew there was an infinite recursive call, I wanted to know where – p00f May 17 '20 at 13:40
  • Got it @dan1st! – p00f Feb 03 '21 at 10:23

0 Answers0