How to solve 5x5 grid with 16 diagonals?

I have a grid 5x5 and I have to fill 16 little squares with a diagonal

Rules

  • You cannot place more than 1 diagonal in each square
  • The diagonals cannot touch each other (example bellow)

not allowed

Click here to view the solution

But what I seek here is a mathematical solution for this puzzle, I don't even know where to start looking for information.

Can someone explain how do I solve this puzzle using equations?


Think of it this way: You have a 5x5 array, and you need to fill it with values 0, 1, or -1, with three restrictions:

  1. The cells sharing an edge with a cell with a $\pm 1$ cannot contain a $\mp 1$.
  2. If a cell contains a 1, the adjacent cells to the top-left and bottom-right cannot also contain a 1.
  3. If a cell contains a -1, the adjacent cells to the bottom-left and top-right cannot also contain a -1.

Here, cells containing a "1" have a diagonal running top-left to bottom-right, and cells containing a "-1" have a diagonal running the other way. Cells containing 0 do not have a diagonal at all.

So, for the example grid you provide, you start with the grid:

$$ \begin{matrix}0&0&-1&0&0\\0&-1&0&-1&1\\0&0&0&0&0\\0&0&0&0&0\\0&0&0&0&0\end{matrix} $$

I don't think there's "a mathematical solution" to it - that is, I doubt that one could immediately find the "right answer" without some level of trial and error. But it does give a good starting point to automating the search, if you want to try solving it computationally.


I have written a brute force search, which seems to work. But it would take days to inspect the $3^{25} = 847,288,609,443$ configurations or reimplementation of some parts in C.

Some feasible solutions from a random walk:

[ 45.33%] [1100201102001021010210022]  found: len = 14 [max = 14]

 / / . . \
 . / / . \
 . . / . \
 / . / . \
 / . . \ \

[ 41.74%] [1020210202102021101001001]  found: len = 14 [max = 14]

 / . \ . \
 / . \ . \
 / . \ . \
 / / . / .
 . / . . /

[ 99.59%] [2222200000102011000011111]  found: len = 14 [max = 14]

 \ \ \ \ \
 . . . . .
 / . \ . /
 / . . . .
 / / / / /

[ 38.82%] [1011110001111010000011111]  found: len = 15 [max = 15]

 / . / / /
 / . . . /
 / / / . /
 . . . . .
 / / / / /

The solutions are (computer generated, main diagonal encoded as +1): cost = 16 $$ \begin{matrix}1&0&-1&-1&-1\\1&0&-1&0&0\\1&1&0&1&1\\0&0&-1&0&1\\-1&-1&-1&0&1\end{matrix} $$ and $$ \begin{matrix}1&1&1&0&-1\\0&0&1&0&-1\\-1&-1&0&-1&-1\\-1&0&1&0&0\\-1&0&1&1&1\end{matrix} $$ See also A264041 (solutions cost), A264667 (number of solutions)