MAIN CLASS

**************************************************

import java.io.*;

class MazeCell {
    public int x, y;
    public MazeCell() {
    }
    public MazeCell(int i, int j) {
        x = i; y = j;
    }
    public boolean equals(MazeCell cell) {
        return x == cell.x && y == cell.y;
    }
}

class Maze {
    private int rows = 0, cols = 0;
    private char[][] store;
    private MazeCell currentCell, exitCell = new MazeCell(), entryCell = new MazeCell();
    private final char exitMarker = ‘e’, entryMarker = ‘m’, visited = ‘.’;
    private final char passage = ‘0′, wall = ‘1′;
    private Stack mazeStack = new Stack();
    public Maze() {
        int row = 0, col = 0;
        Stack mazeRows = new Stack();
        BufferedReader buffer = new BufferedReader(
                                new InputStreamReader(System.in));
        System.out.println("Enter a rectangular maze using the following "
                + "characters:\nm - entry\ne - exit\n1 - wall\n0 - passage\n"
                + "Enter one line at at time; end with Ctrl-z:");
        try {
            String str = buffer.readLine();
            while (str != null) {
                row++;
                cols = str.length();
                str = "1" + str + "1";  // put 1s in the borderline cells;
                mazeRows.push(str);
                if (str.indexOf(exitMarker) != -1) {
                    exitCell.x = row;
                    exitCell.y = str.indexOf(exitMarker);
                }      
                if (str.indexOf(entryMarker) != -1) {
                    entryCell.x = row;
                    entryCell.y = str.indexOf(entryMarker);
                }
                str = buffer.readLine();
            }
        } catch(IOException eof) {
        }
        rows = row;
        store = new char[rows+2][];       // create a 1D array of char arrays;
        store[0] = new char[cols+2];      // a borderline row;
        for ( ; !mazeRows.isEmpty(); row–)
            store[row] = ((String) mazeRows.pop()).toCharArray();
        store[rows+1] = new char[cols+2]; // another borderline row;
        for (col = 0; col <= cols+1; col++) {
            store[0][col] = wall;         // fill the borderline rows with 1s;
            store[rows+1][col] = wall;
        }
    }
    private void display(PrintStream out) {
        for (int row = 0; row <= rows+1; row++)
            out.println(store[row]);
        out.println();
    }
    private void pushUnvisited(int row, int col) {
        if (store[row][col] == passage || store[row][col] == exitMarker)
            mazeStack.push(new MazeCell(row,col));
    }
    void exitMaze(PrintStream out) {
        currentCell = entryCell;
        out.println();
        while (!currentCell.equals(exitCell)) {
            int row = currentCell.x;
            int col = currentCell.y;
            display(System.out);        // print a snapshot;
            if (!currentCell.equals(entryCell))
                 store[row][col] = visited;
            pushUnvisited(row-1,col);
            pushUnvisited(row+1,col);
            pushUnvisited(row,col-1);
            pushUnvisited(row,col+1);
            if (mazeStack.isEmpty()) {
                 display(out);         
                 out.println("Failure");
                 return;
            }
            else currentCell = (MazeCell) mazeStack.pop();
        }
        display(out);
        out.println("Success");
    }
    static public void main (String args[]) {
        (new Maze()).exitMaze(System.out);
    }
}

********************************************************

Stack class

********************************************************

public class Stack {
    private java.util.ArrayList pool = new java.util.ArrayList();
    public Stack() {
    }
    public Stack(int n) {
        pool.ensureCapacity(n);
    }
    public void clear() {
        pool.clear();
    }
    public boolean isEmpty() {
        return pool.isEmpty();
    }
    public Object topEl() {
        if (isEmpty())
            throw new java.util.EmptyStackException();
        return pool.get(pool.size()-1);
    }
    public Object pop() {
        if (isEmpty())
            throw new java.util.EmptyStackException();
        return pool.remove(pool.size()-1);
    }
    public void push(Object el) {
        pool.add(el);
    }
    public String toString() {
        return pool.toString();
    }
}

**********************************************************

SAVE this two classes in the same folder…K?

Sample Input..

11111111111
10000010001
10100010101
e0100000101
10111110101
10101000101
10001010001
11111010001
101m1010001
10000010001
11111111111

Representations

1 - wall

0 - passages

e - exit

m - mouse