I have the following problem:
A direct return account if and only if, simply swap two adjacent tiles horizontally or vertically, so that both remain on instead.
If the swap two records horizontally or vertically adjacent only one is in place, this does not count as direct exchange.
The number of direct exchange is doubled and that is the value of d (n).
the above is what I try to do in Mathematica, but there is something wrong, then show my code to try to solve this problem
directos[tablero_List] := Module[{prb, directo, i},
prb = Flatten[tablero] /. {0 -> 9};
prb = PadRight[prb, 12, 15];
directo = 0;
For[i = 1, i < 10, i++,
If[i != prb[[i]],
Which[Abs[prb[[i]] - prb[[i + 1]]] == 1 && Mod[i, 3] != 0,
directo++,
Abs[prb[[i]] - prb[[i + 3]]] == 3, directo++];
];
];
directo]
Here are some examples
directos[{{1, 5, 3}, {7, 2, 0}, {4, 8, 6}}]
directos[{{6, 3, 2}, {5, 4, 7}, {1, 0, 8}}]
directos[{{2, 1, 5}, {6, 0, 3}, {8, 7, 4}}]
when running my program it sometimes gives me the correct result and not others. For example, for the following matrix, the answer is bad
directos[{{1, 2, 5}, {6, 7, 8}, {0, 4, 3}}]
I have reviewed the matrix, and the correct result is 0, Since there are no swaps possible. We'll see you then