title: Make Java recursive maze solver more efficient
body: <p><strong>Update</strong></p>
  
  <p>I was able to get my algorithm working by increasing the thread size to a few gigabytes and was able to solve a 1803x1803 maze in a second or two. </p>
  
  <p><a href="https://i.stack.imgur.com/37yrC.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/37yrC.gif" alt="1803x1803"></a></p>
  
  <p><strong>---------------</strong></p>
  
  <p>I started teaching myself recursion yesterday in Java. I created an algorithm that takes a photo of a maze and solves it. However, I get a stack overflow answer when doing mazes that are larger than about 200x200 px because I think the stacks of this algorithm get too long. How can I better this algorithm so that I can input images up to possibly 1000x1000?</p>
  
  <p>Additionally, can you tell me what kind of algorithm I am currently using? I believe this is either DFS, but I am unsure.</p>
  
  <p>Please explain why your solution is more efficient and the idea that it uses.</p>
  
  <p>This is the main class for solving</p>
  
  <pre><code>public class BlackWhiteSolver {
  
  static int[][] solutionSet = new int[203][203];
  static int width, height;
  static String originalImage;
  static int correctX, correctY;
  
  public static void convert() {
  try {
  BufferedImage original = ImageIO.read(new File(originalImage));
  int red;
  int threshold = 2;
  width = original.getWidth();
  height = original.getHeight();
  
      for(int i=0; i&lt;original.getWidth(); i++) {
          for(int j=0; j&lt;original.getHeight(); j++) {
              red = new Color(original.getRGB(i, j)).getRed();
              // 1 = white, 0 = black, 9 = tried, 5 = solved
              if(red &gt; threshold) { solutionSet[i][j] = 1; }
              else { solutionSet[i][j] = 0; }
          }
      }
  
  } catch (IOException e) {e.printStackTrace();}
  }
  
  public BlackWhiteSolver(int solvedX, int solvedY, String pic) {
      correctX = solvedX;
      correctY = solvedY;
      originalImage = pic;
  }
  
  public boolean solve (int row, int column) {
  
          boolean completed = false;
  
  
          if (validPoint(row, column)) {
              solutionSet[row][column] = 9;
  
              if (row == correctX &amp;&amp; column == correctY) {
                  completed = true;
              } else {
                  completed = solve (row+1, column);
                  if (!completed) {
                      completed = solve (row, column+1);
                  }
                  if (!completed) {
                      completed = solve (row-1, column);
                  }
                  if (!completed) {
                      completed = solve (row, column-1);
                  }
              }
              if (completed) {
                  solutionSet[row][column] = 5;
              }
          }
  
          return completed;
      }
  
  private boolean validPoint (int row, int column) {
  
          boolean isValid = false;
          if (row &lt; height-1 &amp;&amp; column &lt; width-1 &amp;&amp; row &gt;= 1 &amp;&amp; column &gt;= 1 ) {
              if (solutionSet[row][column] == 1) {
              isValid = true;
              }
          }
  
          return isValid;
      }
  
  public static void solvedFile() {
      BufferedImage binarized = new BufferedImage(width, height,BufferedImage.TYPE_3BYTE_BGR);
      int newPixel = 0;
      int rgb = new Color(255, 0, 0).getRGB();
      for(int i=0; i&lt;width; i++){
          for(int j=0; j&lt;height; j++)
      {
          if (solutionSet[i][j] == 0) {
              newPixel = 0;
              newPixel = colorToRGB(1, newPixel, newPixel, newPixel);
          } else if (solutionSet[i][j] == 1 || solutionSet[i][j] == 9) {
              newPixel = 255;
              newPixel = colorToRGB(1, newPixel, newPixel, newPixel);
          } else if (solutionSet[i][j] == 5) {
              newPixel = 16711680;
          }
  
          binarized.setRGB(i, j, newPixel);
      }
      }
  
      try { ImageIO.write(binarized, "gif",new File("maze-complete") );} catch (IOException e) {e.printStackTrace();}
  
  }
  
      private static int colorToRGB(int alpha, int red, int green, int blue) {
          int newPixel = 0;
          newPixel += alpha;
          newPixel = newPixel &lt;&lt; 8;
          newPixel += red; newPixel = newPixel &lt;&lt; 8;
          newPixel += green; newPixel = newPixel &lt;&lt; 8;
          newPixel += blue;
          return newPixel;
      }       
  }
  </code></pre>
  
  <p>This is the class that runs the maze</p>
  
  <pre><code>public class BlackWhiteInterface
  {
  
      public static void main (String[] args) {
  
          BlackWhiteSolver puzzle = new BlackWhiteSolver(60, 202, "maze-4.gif");
  
          System.out.println();
  
          puzzle.convert();
  
          if (puzzle.solve(0,34)) {
              System.out.println("completed");
              puzzle.solvedFile();
          } else {
              System.out.println("not possible");
          }
      }
  }
  </code></pre>
  
  <p>Generates correct maze with start and end point</p>
  
  <pre><code>public class MazeBuilder {
  
      static String start = "left";
      static String end = "down";
  
      public static void main(String[] args)
      {
          try
          {
              BufferedImage original = ImageIO.read(new File("mazeInput1.gif"));
              BufferedImage binarized = new BufferedImage(original.getWidth(), original.getHeight(),BufferedImage.TYPE_BYTE_BINARY);
              int red;
              int redRightPixel;
              int redUpPixel;
              int newPixel;
              int threshold = 2;
  
              for(int i=0; i&lt;original.getWidth(); i++)
              {
                  for(int j=0; j&lt;original.getHeight(); j++)
                  {
  
                      red = new Color(original.getRGB(i, j)).getRed();
                      int alpha = new Color(original.getRGB(i, j)).getAlpha();
                      if(red &gt; threshold) { newPixel = 255; }
                      else { newPixel = 0; }
  
                      if (i == 0 || j == 0 || i == original.getWidth()-1 || j == original.getHeight() - 1){
                          newPixel = 0;
  
                          if (end == "left") {
  
                          } else if (end == "right") {
  
                          } else if (end == "up") {
  
                          } else if (end == "down") {
  
                          }
  
  
      /*if (i == 1 || j == 1 || i == original.getWidth()-2 || j == original.getHeight() - 2 &amp;&amp; red &gt; 2) {
          System.out.println("Start Point: (" + i + ", " + j + ")");
      }
      if (i == 0 &amp;&amp; j &gt; 0 &amp;&amp; j &lt; original.getHeight()-1) {
  
  
          redRightPixel = new Color(original.getRGB(i+1, j)).getRed();
  
          if (i == 0 &amp;&amp; redRightPixel &gt; 2) {
              System.out.println("Start Point: (" + i + ", " + j + ")");
              newPixel = 255;
          }
      }*/
  
      /*if (j == original.getHeight()-1 &amp;&amp; i &gt; 0 &amp;&amp; i &lt; original.getWidth()-1) {
  
          redUpPixel = new Color(original.getRGB(i, j-1)).getRed();
  
          if (redUpPixel &gt; 2) {
              System.out.println("End Point: (" + i + ", " + j + ")");
              newPixel = 255;
          }
      }*/
  
                      }
  
                      if (start == "left") {
                          if (i == 1 &amp;&amp; j != 0 &amp;&amp; j != original.getHeight()-1 &amp;&amp; red &gt; 2) {
                              System.out.println("Start Point: (" + i + ", " + j + ")");
                          }
                      } else if (start == "right") {
                          if (i == original.getHeight()-2 &amp;&amp; j != 0 &amp;&amp; j != original.getHeight()-1 &amp;&amp; red &gt; threshold) {
                              System.out.println("Start Point: (" + i + ", " + j + ")");
                          }
                      } else if (start == "up") {
                          if (j == 1 &amp;&amp; i != 0 &amp;&amp; i != original.getWidth()-1 &amp;&amp; red &gt; threshold) {
                              System.out.println("Start Point: (" + i + ", " + j + ")");
                          }
                      } else if (start == "down") {
                          if (j == original.getHeight()-2 &amp;&amp; i != 0 &amp;&amp; i != original.getWidth()-1 &amp;&amp; red &gt; threshold) {
                              System.out.println("Start Point: (" + i + ", " + j + ")");
                          }
                      }
  
                      if (end == "left") {
                          if (i == 1 &amp;&amp; j != 0 &amp;&amp; j != original.getHeight()-1 &amp;&amp; red &gt; 2) {
                              System.out.println("End Point: (" + i + ", " + j + ")");
                          }
                      } else if (end == "right") {
                          if (i == original.getHeight()-2 &amp;&amp; j != 0 &amp;&amp; j != original.getHeight()-1 &amp;&amp; red &gt; threshold) {
                              System.out.println("End Point: (" + i + ", " + j + ")");
                          }
                      } else if (end == "up") {
                          if (j == 1 &amp;&amp; i != 0 &amp;&amp; i != original.getWidth()-1 &amp;&amp; red &gt; threshold) {
                              System.out.println("End Point: (" + i + ", " + j + ")");
                          }
                      } else if (end == "down") {
                          if (j == original.getHeight()-2 &amp;&amp; i != 0 &amp;&amp; i != original.getWidth()-1 &amp;&amp; red &gt; threshold) {
                              System.out.println("End Point: (" + i + ", " + j + ")");
                          }
                      }
  
  
                      newPixel = colorToRGB(alpha, newPixel, newPixel, newPixel);
                      binarized.setRGB(i, j, newPixel);
                  }
              }
              ImageIO.write(binarized, "gif",new File("maze-4") );
          }
          catch (IOException e)
          {
              e.printStackTrace();
          }
      }
      private static int colorToRGB(int alpha, int red, int green, int blue) {
          int newPixel = 0;
          newPixel += alpha;
          newPixel = newPixel &lt;&lt; 8;
          newPixel += red; newPixel = newPixel &lt;&lt; 8;
          newPixel += green; newPixel = newPixel &lt;&lt; 8;
          newPixel += blue;
          return newPixel;
      }
  
  }
  </code></pre>
  
  <p>Example output of a 203 x 203 maze</p>
  
  <p><a href="https://i.stack.imgur.com/vfhRZ.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vfhRZ.gif" alt="203x203 maze"></a></p>