I have been working on some code to guide a 'robot' through a maze with multiple dead ends and 1 correct path to the goal like this:

I have used a stack to record the direction the robot is facing the first time it reaches a square with 3 or 4 possible exits and if all adjacent squares have already been visited, used pop() to make the robot return from the direction it first came from (opposite to direction arrived). At the end of the run the stack contains the direction arrived at all squares on the route to the target. Following the opposite directions of the stack would take the robot from the goal back to the start point. I am struggling to find out how to use this stack so that on the next run the robot will take the optimal path to reach the goal.
Some of my code:
private int pollRun = 0; // Incremented after each pass
private int explorerMode; // 1 = explore, 0 = backtrack
public void exploreControl(IRobot robot) {
byte exits = nonwallExits(robot);
int direction;
switch (exits) { //passes control to respective method
case 1: direction = deadEnd(robot); break;
case 2: direction = corridor(robot); break;
case 3: direction = junction(robot); break;
default: direction = crossroads(robot); break;
}
if (exits == 1) {explorerMode = 0;}
robot.face(direction);
pollRun++;
}
public void backtrackControl(IRobot robot) {
byte exits = nonwallExits(robot);
int direction = IRobot.CENTRE;
switch (exits) { //passes control to respective method
case 1: direction = deadEnd(robot); break;
case 2: direction = corridor(robot); break;
default: direction = junction(robot); break; // do nothing
}
if (exits > 2) {
if (passageExits(robot) > 0){
exploreControl(robot);
explorerMode = 1;
pollRun++;
return;
} else {
robot.setHeading(st.pop());
robot.face(IRobot.BEHIND);
pollRun++;
return;
}
}
robot.face(direction);
pollRun++;
}
public void optimal(IRobot robot) {
byte exits = nonwallExits(robot);
int direction;
int heading;
for(int i = 0; i < st.size(); i++) {
stNew.push(st.pop());
}
if (exits < 3) {
switch (exits) { //passes control to respective method
case 1: direction = deadEnd(robot); break;
default: direction = corridor(robot); break;
}
robot.face(direction);
} else {
robot.setHeading(stNew.pop());
}
}
public void controlRobot(IRobot robot) {
if ((robot.getRuns() == 0) && (pollRun == 0)) {
robotData = new RobotData(); //reset the data store
explorerMode = 1;
}
if (robot.getRuns() = 1) {
optimal(robot);
return;
}
if (robot.getRuns() <= 0 && (nonwallExits(robot) >= 3)
&& (beenbeforeExits(robot) <= 0)) {
st.push(robot.getHeading());
}
if (explorerMode == 1) {
exploreControl(robot);
} else {backtrackControl(robot);}
}
The optimal method shows my attempt at solving it, however all it does is cause the robot to head straight at every junction
For example this maze,

Would leave me with the stack: EAST, EAST, SOUTH, SOUTH, EAST, SOUTH, SOUTH, EAST, EAST, SOUTH, SOUTH, EAST, EAST, EAST, SOUTH, EAST, SOUTH