Java solution for the maze problem