Moving a bot through a maze with Depth First Search This is supposed to be a depth first search. I just took the idea of what it does and made my own version of it. The bot works it's way through the maze, when it finds a dead end, it moves back to original spot, and continues in a different direction than it's already gone until it reaches E the end.

import java.util.Stack;
import java.lang.Comparable;
import java.util.*;

class Main {

  public static int[][] maze = { 
    {9,9,9,9,9,9,9,9,9,9,9,9},
    {9,1,1,1,1,1,1,1,1,1,1,9},
    {9,1,0,1,0,1,1,0,0,0,5,9},
    {9,1,0,0,0,0,0,1,0,1,1,9},
    {9,1,1,0,1,1,0,1,0,0,1,9},
    {9,1,0,0,1,0,0,0,0,0,1,9},
    {9,1,1,0,1,1,1,1,1,0,1,9},
    {9,1,0,0,1,0,0,0,1,0,1,9},
    {9,1,1,0,1,0,1,0,1,1,1,9},
    {9,1,0,0,0,0,1,0,0,0,3,9},
    {9,1,1,1,1,1,1,1,1,1,1,9},
    {9,9,9,9,9,9,9,9,9,9,9,9}
    };

    public static Deque&lt;Coordinate&gt; deadEnd = new ArrayDeque&lt;Coordinate&gt;(10);
    public static Deque&lt;Coordinate&gt; deq = new ArrayDeque&lt;Coordinate&gt;(10);


  public static void main(String[] args) {

    printMaze();
    dfs(2,10);

  }

  public static boolean isDeadEnd(Coordinate coord){
    Iterator it = deadEnd.iterator();
    while(it.hasNext()){
      if(it.next().equals(coord)){
        return true;
      }
    }
    return false;
  }

  public static Coordinate setCoord(int i, int j){
    Coordinate coord = new Coordinate();
    coord.setCoord(i,j);
    return coord;
  }

  public static boolean checkCoord(Coordinate coord){
    return iterateCoord(coord);
  }

  private static boolean iterateCoord(Coordinate coord){
    Iterator it = deq.iterator();
    while(it.hasNext()){
      if(it.next().equals(coord)){
        return true; 
      }
    }
    return false;
  }

  public static int dfs(int i, int j){
    if(i == 9 &amp;&amp; j == 9){
      maze[9][10] =4;
      printMaze();
      System.out.println("You're Free!");
      return 0;
    }
    Coordinate coord = new Coordinate();

if(!checkCoord(coord) &amp;&amp; !isDeadEnd(coord)){

    if(maze[i+1][j] == 0 || maze[i+1][j] == 3){
        coord = setCoord(i+1, j);
        deq.push(coord);
        maze[i+1][j] = 4;
        printMaze();
        return dfs(i+1,j);
    }
    else if(maze[i-1][j] == 0){
        coord = setCoord((i-1),j);
    deq.push(coord);
        maze[i-1][j] = 4;
        printMaze();
        return dfs(i-1,j);
    }
    else if(maze[i][j+1] == 0){
        coord = setCoord(i,(j+1));
        deq.push(coord);
        maze[i][j+1] = 4;
        printMaze();
        return dfs(i,j+1);
    }
    else if(maze[i][j-1] == 0){
        coord = setCoord(i,(j-1));
        deq.push(coord);
        maze[i][j-1] = 4;
        printMaze();
        return dfs(i,j-1);
    }
}

    deadEnd.push(deq.pop());
    int n = deq.peek().getX();
    int m = deq.peek().getY();
   // maze[m][n] = 0;
    return dfs(n,m);
  }

  public static void printMaze() {

    for(int i = 0; i &lt; 12; i++){
      for(int j = 0; j &lt; 12; j++){
        if(maze[i][j]==1){
         System.out.print(" X ");
        }
        if(maze[i][j]==0){
          System.out.print("   ");
        }
        if(maze[i][j]==2){
          System.out.print(" e ");
        }
        if(maze[i][j]==3){
          System.out.print(" E ");
        }
        if(maze[i][j]==4){
          System.out.print(" b ");
        }
        if(maze[i][j]==5){
          System.out.print(" b ");
          maze[i][j] = 1;
        }
      }
      System.out.println();
    }
  }
}



class Coordinate{
  Integer x = 0;
  Integer y = 0;

  public int getX(){
    return x;
  }
  public int getY(){
    return y;
  }
  public void setCoord(int i, int j){
    x = i;
    y = j;
  }
  @Override
  public boolean equals(Object obj) {

    if(obj == null){
      return false;
    }
    if(obj instanceof Coordinate){
      Coordinate test = (Coordinate) obj;
      if(test.x == this.x &amp;&amp; test.y == this.y){
        return true;
      }
    }
    return (this == obj);
  }
  /*
  @Override
  public int hashCode(){
    return x + y * 31;
  }
    */
}
I cleaned up your code, but it appears like your code performs a depth-first search.  It's more common to use a graph to represent the different paths than a queue, but your code works, as best as I can tell.

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;

public class DFSMaze {

    public static int[][] maze = { 
            { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }, 
            { 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9 },
            { 9, 1, 0, 1, 0, 1, 1, 0, 0, 0, 5, 9 }, 
            { 9, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 9 },
            { 9, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 9 }, 
            { 9, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 9 },
            { 9, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 9 }, 
            { 9, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 9 },
            { 9, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 9 }, 
            { 9, 1, 0, 0, 0, 0, 1, 0, 0, 0, 3, 9 },
            { 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9 }, 
            { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 } 
            };

    public static Deque&lt;Coordinate&gt; deadEnd = 
            new ArrayDeque&lt;Coordinate&gt;(10);
    public static Deque&lt;Coordinate&gt; deq = 
            new ArrayDeque&lt;Coordinate&gt;(10);

    public static void main(String[] args) {
        printMaze();
        dfs(2, 10);
    }

    public static boolean isDeadEnd(Coordinate coord) {
        Iterator&lt;Coordinate&gt; it = deadEnd.iterator();
        while (it.hasNext()) {
            if (it.next().equals(coord)) {
                return true;
            }
        }
        return false;
    }

    public static Coordinate setCoord(int i, int j) {
        return new Coordinate(i, j);
    }

    public static boolean checkCoord(Coordinate coord) {
        Iterator&lt;Coordinate&gt; it = deq.iterator();
        while (it.hasNext()) {
            if (it.next().equals(coord)) {
                return true;
            }
        }
        return false;
    }

    public static int dfs(int i, int j) {
        if (i == 9 &amp;&amp; j == 9) {
            maze[9][10] = 4;
            printMaze();
            System.out.println("You're Free!");
            return 0;
        }
        Coordinate coord = new Coordinate();

        if (!checkCoord(coord) &amp;&amp; !isDeadEnd(coord)) {

            if (maze[i + 1][j] == 0 || maze[i + 1][j] == 3) {
                coord = setCoord((i + 1), j);
                deq.push(coord);
                maze[i + 1][j] = 4;
                printMaze();
                return dfs(i + 1, j);
            } else if (maze[i - 1][j] == 0) {
                coord = setCoord((i - 1), j);
                deq.push(coord);
                maze[i - 1][j] = 4;
                printMaze();
                return dfs(i - 1, j);
            } else if (maze[i][j + 1] == 0) {
                coord = setCoord(i, (j + 1));
                deq.push(coord);
                maze[i][j + 1] = 4;
                printMaze();
                return dfs(i, j + 1);
            } else if (maze[i][j - 1] == 0) {
                coord = setCoord(i, (j - 1));
                deq.push(coord);
                maze[i][j - 1] = 4;
                printMaze();
                return dfs(i, j - 1);
            }
        }

        deadEnd.push(deq.pop());
        int n = deq.peek().getX();
        int m = deq.peek().getY();
        // maze[m][n] = 0;
        return dfs(n, m);
    }

    public static void printMaze() {
        String chars = " XeEbb";
        for (int i = 0; i &lt; 12; i++) {
            for (int j = 0; j &lt; 12; j++) {
                int k = maze[i][j];
                if (k &lt; 9) {
                    System.out.print(" " + chars.charAt(k));
                    if (maze[i][j] == 5) {
                        maze[i][j] = 1;
                    }
                }
            }
            System.out.println();
        }
    }

}

class Coordinate {
    private int x;
    private int y;

    public Coordinate() {
        x = 0;
        y = 0;
    }

    public Coordinate(int i, int j) {
        x = i;
        y = j;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof Coordinate) {
            Coordinate test = (Coordinate) obj;
            if (test.x == this.x &amp;&amp; test.y == this.y) {
                return true;
            }
        }
        return (this == obj);
    }

}