For the algorithm you could use backtracking (EDIT although it does not quite match your general idea.) You just have to realize your movements are pushed into a virtual stack, and they must be unpushed (and therefore undone.) You might have to implement the stack yourself if the robot is an actually moving object, but you can rely on the call stack if you just want to solve the maze.