How to Solve the Torus Puzzle

In this paper, we consider the following sliding puzzle called torus puzzle. In an m by n board, there are mn pieces numbered from 1 to mn. Initially, the pieces are placed in ascending order. Then they are scrambled by rotating the rows and columns without the player’s knowledge. The objective of the torus puzzle is to rearrange the pieces in ascending order by rotating the rows and columns. We provide a solution to this puzzle. In addition, we provide lower and upper bounds on the number of steps for solving the puzzle. Moreover, we consider a variant of the torus puzzle in which each piece is colored either black or white, and we present a hardness result for solving it.


Introduction
In this paper, we address m by n torus puzzle which is similar to sliding puzzles such as the 15 puzzle.The m × n torus puzzle consists of mn pieces numbered from 1 to mn, and the pieces are placed on a board of m rows by n columns.Initially, for each r ∈ {1, . . ., mn}, the piece numbered r is placed at the pth row and qth column, where p = ⌈r/n⌉ and r ≡ q (mod n).In the puzzle, there are four basic operations, N , S, E, and W (these stand for north, south, east and west).The N (respectively, S) operation shifts the pieces in a column upward (respectively, downward).That is, by applying the N (respectively, S) operation to a column r = (p 1 , p 2 , . . ., p m−1 , p m ) tr , r is changed into (p 2 , p 3 , . . ., p m , p 1 ) tr (respectively, (p m , p 1 , . . ., p m−2 , p m−1 ) tr ), where A tr denotes the transpose of a matrix A. Similarly, the E (respectively, W ) operation rotates a row (p 2 , p 3 , . . ., p n , p 1 ) into (p 1 , p 2 , . . ., p n−1 , p n ) (respectively, (p n , p 1 , . . ., p n−2 , p n−1 )).After performing a sequence of the basic operations, the configuration of the board would differ considerably from the initial configuration.The objective of the torus puzzle is to restore the initial configuration from the scrambled configuration (see Figure 1).Although the 15 puzzle can be intuitively modified into the torus puzzle, the torus puzzle is relatively less popular.This is possibly because it is not easy to implement the torus puzzle on a board, unlike the 15 puzzle.However, the torus puzzle is now gaining popularity owing to the proliferation of touch-panel devices.In fact, the torus puzzle has already been implemented as an application called RotSquare on Mac OS (and is now an iPhone app).
The torus puzzle has not been investigated extensively thus far.In Section 4 in [1], the number of steps for randomizing the configurations of an n by n board is discussed.In [2], it is shown that an n by n board for every even number n, any configuration can be obtained from the initial configuration by showing that any two adjacent pieces can be reversed without disturbing the positions of the others.In this paper, we study the complexity of solving the torus puzzle (and its variant).The remainder of this paper is organised as follows.Section 2 is devoted to a solution of the torus puzzle.After introducing some definitions and notations, we provide the basic tools for solving the torus puzzle, and then we show how the torus puzzle is solved with the basic tools.In Section 3, we discuss the upper and lower bounds on the number of "steps" for obtaining the initial configuration from a scrambled configuration, where successive identical operations applied to a row/column, such as E(r) • . . .• E(r) for some row r, are counted as one step.This form of counting is intuitive when the torus puzzle is implemented on a touch-panel device.The lower bound can be obtained by a standard argument.In Section 4, we consider a variant of the torus puzzle in which the pieces are colored white or black.For the variant, we address the hardness in estimating the minimum number of steps for reaching a given goal configuration from a given start configuration.Unfortunately, we have not determined the hardness for the torus puzzle thus far (it is known that finding the shortest solution for an N ×N extension of the 15 puzzle is NP-hard [3]).

Preliminary
In this section, first we give some definitions and notation, then provide basic tools to solve the torus puzzle, and finally show how to solve the torus puzzle with the basic tools.

Definitions and Notation
This subsection introduces definitions, notation, and conventions.Let O be an operation in {N, S, E, W }. We denote by O i (v) the execution of i consecutive O operations on v, where v is a row (respectively, column) of the board if O ∈ {E, W } (respectively, O ∈ {N, S}).We simply write O(v) as O 1 (v).We call O i (•) for some i and O ∈ {E, W } (respectively, O ∈ {N, S}) a row (respectively, column) operation.It is easy to see that In this paper, a configuration means a placement of the pieces on the board.We will refer to the following configuration as initial configuration and denote it by A: the piece numbered (i − 1)n + j is placed at the ith row and the jth column for each 1 ≤ i ≤ m and 1 ≤ j ≤ n.There are two ways to refer a piece in a configuration.One is by its number and the other is by its position on the board: The piece having number r is referred to as "the piece numbered r", and the piece placed at the ith row and the jth column in a configuration is called "the piece placed at (i, j)" in the configuration.
Let M m,n denote the set of configurations of the m by n board (a configuration of the board can be considered as an m by n matrix in which the pieces are numbered bijectively from {1, 2, . . ., mn}).We say that the board has a configuration X = (x i,j ) ∈ M m,n if the piece placed at (i, j) has number r iff x i,j = r for each 1 ≤ i ≤ m and 1 ≤ j ≤ n.The symbol r k (respectively, c k ) is used to refer to the kth row (respectively, column).We say that the piece numbered r is at its right position in X if x p,q = r, where p = ⌈r/n⌉ and r ≡ q (mod n).For X, Y ∈ M m,n , we write X ≡ {N,S,E,W } Y , or simply X ≡ Y , if Y can be obtained from X by performing a sequence of the basic operations {N, S, E, W }. We thus also write X ̸ ≡ {N,S,E,W } Y , or simply X ̸ ≡ Y , if there is no sequence S of the basic operations such that Y can be obtained from X by applying S. Let be a sequence of the basic operations, where O j ∈ {N, S, E, W } for 1 ≤ j ≤ ℓ and we are assuming that O j ̸ = O j+1 .The integer ℓ and the value ∑ ℓ i=1 k i are referred to as drag number and push number of the sequence S, respectively.We denote the drag and push numbers of S by dn(S) and pn(S), respectively.For configurations X and Y , we denote the values min S dn(S) and min S pn(S) by dn(X, Y ) and pn(X, Y ), respectively, where both minimums are taken over all sequences deriving Y from X. Specially, in the case where Y is the initial configuration A, we simply write dn(X) and pn(X) instead of dn(X, A) and pn(X, A).The value dn(X) and pn(X) are both natural values to measure how far X is from the initial configuration A. In a situation where the torus puzzle is implemented on a touch-panel device, it would be natural to consider a consecutive iterations of the same operation as a single step.In this paper, we use dn(X), that is, for each O ∈ {N, S, W, E} O k (•) is counted as one step, not k steps.

A Basic Tool: Three Elements Rotations
In this section, we introduce a basic tool called three elements rotation, which plays a fundamental role in the solution.
Consider the the following eight patterns of four consecutive operations.Figure 2 is used for showing how the eight patterns work.In Figure 2, there are five points: (the ith row, the jth column), (the i ± pth row, the jth column), (the ith row, the j ± qth column), and there are four triangles consisting of three of these points: A, B, C, and D. It is a routine to check that each pattern makes a clockwise or counterclockwise rotation of the three corners of a triangle A, B, C, or D without other changes of piece positions.
These patterns are referred to as "triangle rotations".
By combining two triangle rotations, a useful rotation can be built as follows.Let X = (x i,j ) be a configuration in M m,n .Let Y be the configuration obtained from X by applying basic operations to the board in the following order: S(c j ), E q (r i ), N (c j ), W q+p (r i ), S(c j ), E p (r i ), N (c j ) for p < j and q ≤ n − j (see Figure 3).
It is easy to see that the only difference between X and Y is the ith row, and the ith row in Y is: no change That is, only the part (x i,j−p , x i,j , x i,j+q ) of the ith row is changed (rotated) into (x i,j , x i,j+q , x i,j−p ).We will refer to this change as three elements rotation on ((i, j), p, q, west) (the east version can be defined similarly).Specially, we denote the three elements rotation on ((i, j), 1, 1, west) by T W (i, j) (the corresponding east version is denoted by T E (i, j)).For example, T W (i, 1) results in

Solution
In this section, we show how to solve the torus puzzle by considering the two cases where mn is even and mn is odd.Not surprisingly, there is a difference between the even case and the odd case.In the even case, for any X ∈ M m,n , X can be changed into the initial configuration A by applying a sequence of the basic operations.But this is not true for the odd case.(A similar thing happened for the 15 puzzle [4].)To describe our solution of the torus puzzle, we need the following lemmas.
Lemma 1. Suppose that n ≥ 2 is an even integer.For any two consecutive pieces x p,q−1 and x p,q in a row of a configuration X = (x i,j ) ∈ M m,n , we can swap the positions of two pieces without causing any other change of position by applying a sequence of basic operations.More precisely, it holds that X ≡ Y (= (y i,j )), where y p,q = x p,q+1 , y p,q+1 = x p,q , and y i,j = x i,j for (i, j) ̸ ∈ {(p, q + 1), (p, q)}.
Proof.Suppose n ≥ 4 (the case of n = 2 is trivial).It is sufficient to show that there is a sequence of basic operations which changes the first row (1, n) in this order, then the first row becomes (x 1,1 , x 1,3 , . . ., x 1,n , x 1,2 ).Finally, execute E(the 1st row), then we have the desired result.
The next lemma is inspired by an argument on the parity of permutations in [4].
Lemma 2. Suppose that mn is an odd integer.For any two consecutive pieces x p,q−1 and x p,q in a row of a configuration X = (x i,j ) ∈ M m,n , we cannot swap the positions of two pieces without causing any other change of position.More precisely, it holds that X ̸ ≡ Y (= (y i,j )), where y p,q = x p,q+1 , y p,q+1 = x p,q , and y i,j = x i,j for (i, j) ̸ ∈ {(p, q + 1), (p, q)}.
Proof.Suppose not, then there is a sequence S of basic operations which swaps the positions.Let h (respectively, v) be the number of row (respectively, column) operations in S, Then, S has h permutations of n elements and v permutations of m elements (i.e., h cycles of length n and v cycles of length m).As any permutation of k ≥ 2 elements can be represented by the production of k −1 transpositions, S can be represented with h•(n−1)+v •(m−1) transpositions.This, however, means that S is even permutation, since both m − 1 and n − 1 are even.However, S should be odd permutation, a contradiction.
We are now ready to demonstrate our solution.Let A = (a i,j ) be the initial configuration.We first show how to obtain a configuration Y = (y i,j ) from any configuration X = (x i,j ) ∈ M m,n such that y i,j = a i,j for 1 ≤ i ≤ m − 1, 1 ≤ j ≤ n, i.e., Y achieves the initial configuration except for the bottom row.Suppose that the pieces numbered 1, . . ., r − 1 are already at their right positions.Let p and q be the integers such that p = ⌈r/n⌉ and r ≡ q (mod n).We now wish to have a configuration in which the pieces numbered 1, . . ., r are at their right positions.Let (s, t) be the position of the piece numbered r in the current configuration.We will refer to the outside of the region placed the pieces numbered 1, . . ., r − 1 as work space.By proper choice of a third corner (u, v) (from work space) and executing a proper triangle rotation on the three corners (p, q), (s, t), and (u, v), we can have the desired configuration.Note that, because of the work space, we can take a third corner properly (see Figure 4).We now show how to achieve the initial configuration from the configuration Y .First, consider the case where mn is even.Without loss of generality, we may assume that n is even.Since the case of n = 2 is easy, we assume n ≥ 4. By applying three elements rotation as many times as necessary, the piece numbered (m − 1)n + 1 can be replaced at its right position (m, 1).We repeat this process for pieces numbered (m−1)n+2, . . ., mn−2.By Lemma 1, we can swap the positions of pieces numbered mn − 1 and mn if necessary (i.e., if the last row is (m − 1)n + 1, . . ., mn − 2, mn, mn − 1).Then, we have the initial configuration A. Now consider the case where mn is odd.In a similar fashion as above, the last row in Y can be changed into (m − 1)n + 1, . . ., mn − 2, x, y) by applying three elements rotation as many times as necessary.If we assume that X ≡ A (i.e., Y ≡ A), then, by Lemma 2, it is impossible to be (x, y) = (mn, mn − 1).Thus, (x, y) should be (mn − 1, mn), which means that we have already had the desired result.

Upper and Lower Bounds of diam(M m,n )
In this section, we consider the following problem: How many steps are enough to derive a configuration Y ∈ M m,n from a configuration X ∈ M m,n .That is, we try to evaluate the value max X,Y ∈Mm,n dn(X, Y ).We call the value the diameter of M m,n and denote it by diam(M m,n ).Next theorem gives simple upper and lower bounds for diam(M m,n ).(For pebble games which are generalization of the 15 puzzle, a similar study can be found in [5].)Theorem 1.The following statements hold: 1.For any configurations X, Y ∈ M m,n with X ≡ Y , dn(X, Y ) ≤ 4mn + c • max{m, n} for some constant c. 2. For a given 0 < ε < 1, there are integers m and n such that for any configuration X ∈ M m,n there is a configurations Y ∈ M m,n for which dn(X, Y ) ≥ εmn.
Proof.We first show the first statement.Since triangle rotation and three elements rotation take four and seven steps, respectively, according to our solution described in the previous section, we can have an upper bound: diam(M m,n ) ≤ 4mn + c • max{m, n} for some constant c.We now show the second statement.Let us consider first the case where mn is even.Note that ) mn < (mn)! ≤ (2mn) d+1 −1 2mn−1 ≤ (2mn) d+1 , it follows that mn log(mn)−1 log 2+log(mn) − 1 < d.This completes the proof of the even case for the second statement.

. (mn)! configurations
Let us consider now the case where mn is odd.The second statement holds also for the odd case.This can be proved by the almost same way.The difference is the number of configurations obtained from X, that is, |{Y : Y ≡ X}| = |M m,n |/2 holds for the odd case.Clearly, the difference does not affect the result of calculation.
By applying a similar argument to evaluating the push number, we have that pn(X, Y ) is O(mn • max{m, n}) for any pair (X, Y ) with X ≡ Y , and for any X there is Z such that pn(X, Z) is mn(1 − o( 1)).

Hardness
In this section, we consider a variant of the torus puzzle in which the pieces are colored with white or black.The end of the variant is to derive from a given start configuration to a given goal configuration.
For the variant, we show that the following problem is NP-hard by reduction from 3-PARTITION: For given a start configuration X, a goal configuration Y , and an integer k, whether or not dn(X, Y ) ≤ k?We will refer to this problem as BWP (Black & White Problem).

3-PARTITION
Since 3-PARTITION is strongly NP-complete [6], without loss of generality we can assume that each ℓ i is given in unary representation.
Observation 1.Let r be a row in the board.If r has i black pieces, then, in order to make r so that r has i − j black pieces, at least j column operations are needed.This is because that each column operation remove at most one black piece from r.For the same reason (just thought black piece as white and vice versa), if r has i black pieces, then in order to make r so that r has i + j black pieces, at least j column operations are needed.The same holds true for a column.Theorem 2. BWP is NP-hard.

Without loss of generality, we may assume
The board in G has 9m + 1 rows, ℓ 3m + 3(mB + m − 1) columns, and mB + m − 1 black pieces.The (6m + 1)th row plays an important role in this proof.As we shall show later, the m − 1 black pieces in the (6m + 1)th row in the start configuration cannot move both horizontally and vertically.The other mB black pieces initially placed at the upper left in the start configuration will be replaced in the (6m+1)th row in the goal configuration.
Let S be a sequence of row and column operations such that S derives the goal configuration from the start configuration.Let C be the set of configurations which appear during the steps derived by S.
[I is a yes instance ⇒ G is a yes instance.] Since I is a yes instance, there are m disjoint sets A 1 , . . ., A m such that ∑ ℓ∈A i ℓ = B for each 1 ≤ i ≤ m.For each A i = {ℓ x , ℓ y , ℓ z }, first shift the black pieces corresponding ℓ x , ℓ y , ℓ z horizontally as shown in Figure 8. Then shift the black pieces corresponding ℓ x , ℓ y , ℓ z vertically.This can be done with 3m + mB steps.Hence, G is a yes instance.
Since the first column has 3m black pieces in the start configuration and no black piece in the goal configuration, by Observation 1, S has at least 3m row operations.Similarly, as the (6m + 1)th row has m − 1 black pieces in the start configuration and mB + (m − 1) black pieces in the goal configuration, S has at least mB column operations.Thus, as dn(S) ≤ 3m + mB, S has exactly 3m row operations and mB column operations.Hence, each of the 3m row operations should sweep out of a black piece from the first column, and each of mB column operations should bring a black piece into the (6m + 1)th row.Furthermore, once a black piece is in the (6m + 1)th row, the piece should be in the row during the steps derived by S, since S has exactly mB column operations.
Let c 1 , . . ., c m−1 denote the black pieces in the (6m + 1)th row and b 1 , . . ., b mB denote the other black pieces in the start configuration.We now give the following claims.Note that each c i cannot be shifted vertically since a black piece is in the (6m + 1)th row cannot swept out from the (6m + 1)th row.
Proof.If not so, there are two black pieces at least mB + m − 1 column away from each other.Since there are no such black pieces at least mB + m − 1 column away from each other in the (6m + 1)th row in the goal configuration, one of the two pieces should sweep out from the row.This, however, is not allowed as explained above, a contradiction.
Claim 2. In S, no operation is applied to the first column and also no operation is applied to the (6m + 1)th row.
Proof.We first show that if the first column is shifted vertically at some step, then the (6m+1)th row has been shifted horizontally before that step.Suppose that, in S, there is a column operation applied to the first column.Note that, by this column operation, a black piece is placed at (6m + 1, 1).From Claim 1, before applying this operation, c 1 (or c m−1 ) should be already in the jth column for some j ∈ R, where As each c i cannot move vertically, c 1 (or c m−1 ) should be moved by a row operation.
On the other hand, to apply a row operation to the (6m + 1)th row, the piece placed at (6m + 1, 1) should be black, because a black piece in the first column must be swept out from the first column whenever a row operation is executed.However, in order to place a black piece at (6m + 1, 1) without applying a row operation to (6m + 1)th row, the first column should be shifted vertically, so we have a deadlock.The deadlock completes the proof.
We next show that each b i first move horizontally, then move vertically.Let I denote the set of indexes k such that ℓ 3m + mB + m ≤ k ≤ ℓ 3m + 2mB + 2m − 2 and the kth column in the start configuration has no black piece.Note that |I| = mB.Since S has exactly mB column operations, by Claim 2 (i.e., the (6m + 1)th row cannot shift horizontally), in S, each column c k with k ∈ I must be applied exactly one column operation.Similarly, since S has exactly 3m row operations, by Claim 2 (i.e., the first column cannot shift vertically), in S, each row r k with 1 ≤ k ≤ 3m must be applied exactly one row operation.Thus, b i first must move to a column c k with k ∈ I, then move vertically.
We now claim that in any configuration C ∈ C, each column c j with j ∈ I has at most one black piece.If not so, there is a column c j with j ∈ I such that c j has more than one black piece in a configuration C ∈ C. One of those black pieces must be swept out from c j by applying a row operation.Let p be such a black piece swept out from c j .Since p has two row operations, p moves first horizontally, second vertically, then horizontally (Recall that each row r k , 1 ≤ k ≤ 3m, has exactly one row operation).Let O be the row operation for the second horizontal movement of p, and x the index such that O apply to the xth row r x .Then, x must be 3m + 1 < x < 9m + 1, because p has moved vertically to a row between the (3m + 2)th row and the 9mth row.But, since row operations are applicable only to r k for 1 ≤ k ≤ 3m, p now cannot move horizontally.
As a result, each b i first move to a column c k with k ∈ I, then move vertically to (6m + 1, k).Moreover, in any configuration in C, each column c k with ℓ 3m + mB + m ≤ k ≤ ℓ 3m + 2mB + 2m − 2 has at most one black piece.This means that ℓ 1 , . . ., ℓ 3m must be able to partitioned into m groups that all have the same sum B. Therefore, I is a yes instance.

Discussion
In this paper, we showed that 1 ≤ lim n→∞ diam(Mn,n) n 2 ≤ 4. It would be interesting to improve the lower or upper bounds, especially the lower bound.One way to obtain upper bounds is to find integers p, q, d 1 , d 2 , b, and a "potential" function ϕ : M n,n → {0, 1, 2, . ..} satisfying the following conditions: (1) ϕ(X) = 0 iff X = A (the potential of the initial configuration is zero), ( 2) ∀X ̸ = A with ϕ(X) ≥ b, ∃Y such that [dn(X, Y ) ≤ d 1 ] and [ϕ(X) − ϕ(Y ) ≥ q] (if the potential is at least some threshold, say b, then one can decrease it by q using at most d 1 steps.),(3) if ϕ(X) < b, then dn(X, A) ≤ d 2 (if the potential is less than b, then we can always reach A within d 2 steps.), and ( 4) ϕ(Y ) ≤ p for every Y (the maximum potential is p.)This would imply that the upper bound of dn(M n,n ) ≤ ⌈ p−b q ⌉d 1 + d 2 .Actually, our upper bound was obtained in this manner by setting ϕ(X) = r iff the pieces numbered q ≤ r are placed at their right positions and the piece numbered r + 1 is not placed at its right position.We believe that our upper bound can still be improved to 3, even if one uses the same ϕ.However, we suspect that more sophisticated ϕ will be needed to find a better upper bound.On the other hand, our lower bound was obtained by considering the graph G = (M m,n , {{X, Y } | Y ∈ N (X)}).It would be interesting to improve the lower bound by using the structural information of G.

Figure 1 .
Figure 1.An example of play.

Figure 4 .
Figure 4. Work space and third corner

1 2mn− 1 (
For a configuration P in M m,n , let N (P ) denote the set of configurations Q obtained from P by executing one row or column operation (namely, Q can be obtained from P by applying O k for O ∈ {N, S, E, W } and some integer k).Let d = max W ∈Mm,n dn(X, W ). Since |N (P )| is at most 2mn, |M m,n | is at most (2mn) d+1 −see Figure 5).Hence, as |M m,n | = (mn)!, (mn)! ≤ (2mn) d+1 −1

Figure 5 .
Figure 5.The relation between d and mn.

Claim 1 .
For any configuration C ∈ C, in the (6m + 1)th row in C, there is no two black pieces at least mB + m − 1 column away from each other.

Figure 8 .
Figure 8. Procedure of move of the black pieces corresponding A i .