# Remove the magic from a 3x3 Magic Square

It's commonly stated that there is only one solution to the 3x3 magic square. This is simply because every variant found is simply a rotation, reflection or both of any other variant. Once such variant is:

#### Objective

Now, your goal is to make it less interesting, albeit, in an interesting way. Your objective is simple, rearrange the magic square so that the digits can be read in sequence from $$1$$ to $$9$$ starting in the top left corner, to the bottom right corner:

#### How to Play

To rearrange the tiles, you'll need to swap them, one by one into their correct positions. For example, the $$1$$ tile can be swapped with the $$8$$ tile, effectively placing the $$1$$ tile in its correct position:

However, be very careful on which tiles you swap as there are two important rules to swapping tiles:

1. Each tile can only be swapped the number of times its face represents.
2. When swapping tiles, the lower face value is deducted from the higher face value's remaining moves.

For clarity, let's use the $$8$$ tile as an example. At the start, it can be moved 8 times before it becomes locked into place. Swapping it with the $$1$$ tile reduces the number of times it can be moved going forward to 7. To clarify the second rule, imagine that we now swap the $$8$$ tile with the $$6$$ tile:

Two things happen here; the $$6$$ tile now has $$5$$ moves remaining, and the $$8$$ tile now has $$1$$ move remaining. This isn't optimal because the $$8$$ tile is nowhere near where it needs to be.

#### Movement Restrictions

Tiles can be swapped if they are adjacent to each other. Additionally, outside tiles are considered adjacent. Diagonal movements are not allowed. For example, the $$8$$ tile can be swapped with the $$1$$, $$6$$, $$3$$ and $$4$$ tiles initially.

Can you remove the magic from a 3x3 magic square?

• Given that the grid is 3x3, it might be clearer rather than stating "Tiles can be swapped if they are adjacent ..." with additional redefinitions of "adjacent" to simply state "Tiles may be swapped with any other tile in the same row or the same column". Depends on whether it would be intended to be extended to larger squares of course... Sep 29 at 12:29
• @Steve That’s a fair point! I think I went with that verbiage because it started as a 4x4 but I scaled it down to make the initial puzzle better received. Sep 29 at 12:34

Can we remove the magic?

Yes we can!

This is the path I took to make a magic square into the grid:

First, let's take the magic square given:

We need to make it to:

Notice

How 5 is already correct. We don't want to touch it, so that's why the outside adjacent rule is here.

So with that,

We need to get 1 into place, and it needs to be first because no matter what happens, 1 is going to get misplaced to a position where we can't place it back in one move. If I understand the rules, this is what will happen:

Note that the brackets mean how many moves are left

After that,

We need to get 2 in place. We can go up and left, but that only solves 2. If we go left and up, we also get to solve 8 and 9 at the same time.

That helps out a ton, as the rest is pretty trivial:

From there, we swap 7 and 3 now as the 7 needs to start coming over. Notice we can't swap 4 and 3 because 3 will be stuck, and 6 can't swap with 7, as 7 will get stuck.

Then,

Aha! The 3 and 6 can now slip into place, both becoming into the right place.

Lastly,

We finish it off, making everything in the right place.

Well, say goodbye to the magic in the 3 by 3 magic square, and say hello to a normal 3 by 3 square!

• Re: "We need to get 1 into place, and it needs to be first because..." - I don't see how you ruled out the possibility that [something else] swaps with the 8 before the 1 swaps with [something else]. Perhaps there are several other solutions? Sep 29 at 12:20
• @Steve there is, by design, more than one way to solve this, though there are a limited number of ways to solve it. You are correct that the $1$ tile isn't required to be moved first (my solution worked backwards from $9$), but Stevo does have a nice solution here. Sep 29 at 13:26
• @Steve 8 needs to swap with 1 due to the fact that you can't quite swap 8 with anything worthwhile: The 6, 3,7,and 4 tile are already 3 swaps away from being solved, with no other tiles in the way. Even so, lets swap 3 with 8. After 2 comes up, or even 6 and 3 swap, one of them gets stuck at the bottom. We can't get them back up, so its a clash. The first thing I realised was the 6, 3, 7 and 4 tile being perfectly placed. Oct 1 at 7:31

As a supplementary answer I implemented a breadth-first search with duplicate suppression (so e.g. if a state is reachable by swapping A with B and then C with D, it only keeps one of the A<>B C<>D or C<>D A<>B paths), scored by the "total moves left" which monotonically decreases with each swap.

Assuming no errors this reveals that

There are two distinct solutions (which don't share any intermediate states). Both also do the 8 to 1 swap first as Stevo asserted was necessary.

The (hopefully self-explanatory) output from this is below. Source code at https://pastebin.com/5xXQtPMv could theoretically be trivially modified to provide an answer to the follow-on puzzle, but in practice needs to be much better optimised for lower memory usage.

Starting state [total moves left: 45]:
8₈ | 1₁ | 6₆
----+----+----
3₃ | 5₅ | 7₇
----+----+----
4₄ | 9₉ | 2₂

Starting on working set #45, which has 1 members...
Starting on working set #44, which has 0 members...
Starting on working set #43, which has 4 members...
Starting on working set #42, which has 4 members...
Starting on working set #41, which has 4 members...
Starting on working set #40, which has 18 members...
Starting on working set #39, which has 32 members...
Starting on working set #38, which has 26 members...
Starting on working set #37, which has 94 members...
Starting on working set #36, which has 97 members...
Starting on working set #35, which has 193 members...
Starting on working set #34, which has 204 members...
Starting on working set #33, which has 486 members...
Starting on working set #32, which has 593 members...
Starting on working set #31, which has 834 members...
Starting on working set #30, which has 1139 members...
Starting on working set #29, which has 1736 members...
Starting on working set #28, which has 2359 members...
Starting on working set #27, which has 3173 members...
Starting on working set #26, which has 3671 members...
Starting on working set #25, which has 5465 members...
Starting on working set #24, which has 6223 members...
Found solution!
... after swaps: R1C1<>R1C2, R3C2<>R3C3, R3C2<>R1C2, R2C3<>R2C1, R1C3<>R2C3, R2C1<>R3C1
1₀ | 2₀ | 3₁
----+----+----
4₃ | 5₅ | 6₃
----+----+----
7₀ | 8₅ | 9₇

Starting on working set #23, which has 8313 members...
Starting on working set #22, which has 10303 members...
Starting on working set #21, which has 10968 members...
Starting on working set #20, which has 14641 members...
Starting on working set #19, which has 15971 members...
Starting on working set #18, which has 18022 members...
Starting on working set #17, which has 20375 members...
Starting on working set #16, which has 20095 members...
Starting on working set #15, which has 22740 members...
Starting on working set #14, which has 24254 members...
Starting on working set #13, which has 24903 members...
Starting on working set #12, which has 22867 members...
Starting on working set #11, which has 20175 members...
Found solution!
... after swaps: R1C1<>R1C2, R3C3<>R1C3, R1C2<>R1C3, R2C3<>R2C1, R2C3<>R3C3, R3C3<>R1C3, R2C1<>R3C1, R3C2<>R3C3
1₀ | 2₀ | 3₀
----+----+----
4₃ | 5₅ | 6₁
----+----+----
7₀ | 8₁ | 9₁

Starting on working set #10, which has 19590 members...
Starting on working set #9, which has 17287 members...
Starting on working set #8, which has 14352 members...
Starting on working set #7, which has 11581 members...
Starting on working set #6, which has 8077 members...
Starting on working set #5, which has 5765 members...
Starting on working set #4, which has 3029 members...
Starting on working set #3, which has 1661 members...
Starting on working set #2, which has 646 members...
Starting on working set #1, which has 217 members...

• I love this answer! As such, I'm removing the no-computers tag from the follow on. Sep 30 at 18:39