All right. For those of you who have been waiting patiently (and silently), I have calculated the expected number of transitions before the robot hits the given point.
For those who absolutely can’t wait, it’s 343. More generally, for an n x n grid, the expected number of transitions is 1+(n-1)(n-2).
“Well, that’s fine and dandy,” you say, “but what’s a transition? And where did you get these numbers?”
In order to explain, I need to first document my assumptions, then explain the Markov chain I created.
Here are my assumptions:[list=1][li] Both the initial point and the terminal point are on the interior of the grid.[/li][li] The line containing the initial point and the terminal point is not parallel to either side of the grid.[/li][li] The robot has a 1/4 chance of facing in any of the four cardinal directions at the beginning.[/li][li] The robot turns left with probability 1/2, and right with probability 1/2.[/li][li] The number of steps that the robot backs up is a uniform random variable over the set {1,…,19} (of course, this set has only integers).[/list=1]I don’t know that this is exactly what the OP coded up, but these assumptions made my analysis a lot simpler.[/li]
So as I said earlier, I was using a 6-state Markov chain to model this behavior. The states are as follows:[list=1][li] The robot is at the initial point.[/li][li] The robot is on the west wall.[/li][li] The robot is on the north wall.[/li][li] The robot is on the east wall.[/li][li] The robot is on the south wall.[/li][li] The robot is on the terminal point (think of it as a pit into which the robot falls).[/list=1]A transition is just a change of state. Not all transitions are going to be of equal time, but that doesn’t matter for the Markov chain. If someone is bored, they might want to consider the expected time needed to go through 343 transitions.[/li]
I’m not going to try to code up the matrix of transition probabilities and make it look nice; rather, I’ll just give enough information to recreate it. I should add for those of you who are unfamiliar that the probabilities must add up to 1, so any unstated transition probabilities are equal to 0.
From state 1, the robot has a 1/4 chance of reaching each of states 2, 3, 4, and 5. From state 6, the robot has no chance of ever leaving; it stays in state 6 with probability 1 (in this case, we have certainty. ;)).
When the robot is on the wall, the situation is a bit more complicated. It has probability zero of either staying on that wall or returning to the initial point. It has probability 1/(n-2) of reaching the opposite wall, and probability 1/((n-1)(n-2)) of entering state 6. Its probability of reaching either adjacent wall is 1/2-n/(2(n-1)(n-2))). It is left as an exercise to the reader to derive these; one might be tempted to use conditional expectations.
From here, it’s a simple matter to calculate the expectation given at the beginning. One simply need know what the fundamental matrix of a Markov chain is, how to calculate it, and how to read its entries. Of course, the best resource for this is Google.
So that’s my analysis. Questions?