So I was looking at a few past-years' papers from the ZIO (an IOI qualifier held here in India), and I found this question:
I think this is the same as finding the number of paths of (let's take (a)) length 8 starting at vertex 4 on this graph:
1 ---- 2 ---- 3
| | |
4 ---- 5 ---- 6
| | |
7 ---- 8 ---- 9
How can this be done systematically, and (more importantly) on pen and paper? (I have an extremely inelegant answer which I don't like. I'll post it below.)
To do (a), you can start with $$\pmatrix{0&1&0\cr0&0&0\cr0&0&0\cr}$$ Then replace each entry with the sum of all its neighbors, $$\pmatrix{1&0&1\cr0&1&0\cr0&0&0\cr}$$ Do it again; $$\pmatrix{0&3&0\cr2&0&2\cr0&1&0\cr}$$ The eighth matrix you get this way will tell you, for each digit $d$, how many codes of length 8 there are starting with 2 and ending with $d$. So you just have to add up all the entries in the 8th matrix to get the answer.