Minimum cost of forming a magic square

606 Views Asked by At

Hello i have a question regarding finding the minimum cost of converting a 3 x 3 matrix in to a magic square.

So i have a simple question, why can't we solve it by finding the sum of the each row then subtracting it from 15 (taking the absolute value)? I mean, each row should sum up to 15 anyway, right?

For a case like this:

4 5 8

2 4 1

1 9 7

the answer is 14 and not 12 but i don't understand why.

1

There are 1 best solutions below

3
On

The minimum cost to make each row and column sum to $15$ is indeed $12$. But that ignores the additional restriction of magic squares that each integer in $\{1,\dots,9\}$ should appear exactly once.

In fact, even imposing that the final values are in the interval $[1,9]$ (and not necessarily integer or distinct) yields minimum cost $14$.