I have a $N \times N$ grid, each cell initialized with an integer between $0$ and $5$. If I select one cell, each adjacent cell (8 cells if not on an edge or a corner) - but not the selected one- are incremented by $1$ then taken $\text{mod } 5$. I need to find the cells to select to get from the initial state to a given targeted state. I assume there is always a solution but I don't know if it is unique.
I have done this heuristically, but my implementation is very inefficient, and I wondered if linear algebra could be used here. In case it can be done with LA, any leads to help me formulate the problems would be very appreciated.
Thank you in advance for your help
Too long for a comment.
Let us consider the case $3\times 3$. Define $9$ matrices according to $$ \Delta_{1,1}=\begin{pmatrix}0& 1&0 \\ 1&1&0 \\ 0&0&0 \end{pmatrix}\\ \Delta_{1,2}=\begin{pmatrix}1& 0&1 \\ 1&1&1 \\ 0&0&0 \end{pmatrix}\\ \Delta_{1,3}=\begin{pmatrix}0& 1&0 \\ 0&1&1 \\ 0&0&0 \end{pmatrix}\\ \Delta_{2,1}=\begin{pmatrix}1& 1&0 \\ 0&1&0 \\ 1&1&0 \end{pmatrix}\\ \Delta_{2,2}=\begin{pmatrix}1& 1&1 \\ 1&0&1 \\ 1&1&1 \end{pmatrix}\\ \Delta_{2,3}=\begin{pmatrix}0& 1&1 \\ 0&1&0 \\ 0&1&1 \end{pmatrix}\\ \Delta_{3,1}=\begin{pmatrix}0& 0&0 \\ 1&1&0 \\ 0&1&0 \end{pmatrix}\\ \Delta_{3,2}=\begin{pmatrix}0& 0&0 \\ 1&1&1 \\ 1&0&1 \end{pmatrix}\\ \Delta_{3,3}=\begin{pmatrix}0& 0&0 \\ 0&1&1 \\ 0&1&0 \end{pmatrix} $$ Let us record the state into a $3\times 3$ matrix (with entries in $\mathbb Z/5\mathbb Z$), let the initial state be $I$. Then, any reachable state $F$ is of the form $$ F=I+\sum_{i,j=1}^3n_{i,j}\Delta_{i,j}. $$ Introduce the linear transformation $\mathscr L$ of $Mat(\mathbb Z/5\mathbb Z,3\times 3)$ onto itself by $$ N\mapsto \sum_{i,j=1}^3n_{i,j}\Delta_{i,j}. $$ Ordering the entries of $N$ as a $9$-dimensional vector $(n_{11},n_{12},n_{13},n_{21},n_{22},n_{23},n_{31},n_{32},n_{33})$, $\mathscr L$ is represented by the matrix $$ L=\left( \begin{array}{ccccccccc} 0 & 1 & 0 & 1 & 1 & 0 & 0 & 0 & 0 \\ 1 & 0 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 1 & 1 & 0 & 0 & 0 \\ 1 & 1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 \\ 1 & 1 & 1 & 1 & 0 & 1 & 1 & 1 & 1 \\ 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 1 \\ 0 & 0 & 0 & 1 & 1 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 & 1 & 1 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 & 1 & 1 & 0 & 1 & 0 \\ \end{array} \right) $$ It can be checked that $\det L=0$, hence there are initial and final state which cannot be joined by this process.
For $1\times 1$ and $2\times 2$ the corresponding linear transformation $\mathscr L$ is instead invertible. Probably with some work (and/or some smart ideas) one can understand for which $n$ the relevant matrix $L$ is invertible.
EDIT: in fact, for the case $3\times 3$, the non-invertibility comes from the fact that the matrices $\Delta_{i,j}$ are not l.i.: $$ \Delta_{1,1}-\Delta_{1,3}-\Delta_{3,1}+\Delta_{3,3}=0_{3\times 3}. $$ A similar phenomenon occurs in the $4\times 4$ case: $$ \Delta_{1,1}+\Delta_{1,4} +\Delta_{2,1}-\Delta_{2,2}-\Delta_{2,3}+\Delta_{2,4} +\Delta_{3,1}-\Delta_{3,2}-\Delta_{3,3}+\Delta_{3,4} +\Delta_{1,4}+\Delta_{4,4}=0_{4\times 4} $$ and it is likely to occur for higher $n$ as well.