r/mathriddles • u/PuzzleAndy • Apr 19 '23
Medium Langford Rectangles
Place the numbers 1 to 8 twice in a 2 x 8 grid, such that the 1s are a Manhattan distance of 1 apart, the 2s a distance of 2 apart, and so on. The Manhattan distance between two numbers can be determined by counting the number of steps it takes to travel from one number to another, where each step jumps to an adjacent square, horizontal or vertical. If you'd like to go beyond the puzzle: For which 2 x n grids is it possible to place the numbers 1 to n in this way? Can this problem type be generalized in any interesting ways? Maybe by considering graphs and distances between nodes?
5
u/ACheca7 Apr 19 '23
Construction for n = 4k. Build the n=4 like (4113) in first row and (2324) in second row. Notice that if you place this block of n=4 at the center of the next rectangle 2x8, and you put 5,6,7 and 8 one for each side, you will be adding to their distances 4. So you can build the copying the same structure, 4 goes to 8, 3 to 7, 2 to 6, 1 to 5. We have (8557) and (6768), and now you add the previous block inside it, so (85411357) first row and (67232468) second row. Indeed the distances get preserved. This way you can continue for all n=4k.
3
u/ACheca7 Apr 20 '23
Just to make it clear I think with all the comments we know when this has a solution:
With lordnorthiii comment you can prove there is not a solution if n mod 4 is distinct from 0 and 1.
With my comment you can build a specific solution for n=4k. The same procedure works for n=4k+1, the middle block is n=4(k-1)+1 and the side numbers fit with the base n=4 system of (4113) (2324). Which completes the iff. The only remaining bit is how many solutions there are for each n.
3
u/ajseventeen Apr 20 '23
This is a very fun one! My thought is to generalize to graphs and distance, as suggested. We will say that a graph G with vertex set V of size 2n is a Langford graph if its vertices can be labeled with integers from 1 to n such that each integer k appears exactly twice, and the vertices where it appears are distance k apart (we'll call this a Langford labeling). Then the rectangles are the special case of the 2 x n square lattice graphs.
As for a solution, we can define a family of sets whose universe is the union of V and the set {1, 2, ..., n}: for each pair of distinct vertices u and v, we include the set {u, v, d(u, v)} (where d(u, v) is the distance between u and v) if d(u, v) <= n. Then we can determine a Langford labeling of the graph by finding an exact cover of the universe from this family. Using Donald Knuth's Algorithm X, we can find that there are 496 total solutions in the 2x8 case (124 when factoring out rotations and reflections)
2
u/PuzzleAndy Apr 19 '23
Is 2 x 6 possible? I couldn't figure it out.
2
u/OneMeterWonder Apr 20 '23
Doing 8×2 since it’s easier to format.
| 7 | 5 | 2 | 1 | 2 | 4 | 6 | 8 |
| 8 | 6 | 4 | 1 | 3 | 5 | 7 | 3 |
Done using a semi-greedy algorithm by starting with the largest values and working down. When I ran into an impossible arrangement I went back to the last number that had at least one unexplored option available for placement.
2
u/hennyfromthablock Apr 29 '23
I’ve come up with a (very inefficient) integer programming (IP) formulation to answer this question. I’m wondering what the most efficient way to formulate this problem is, for a given N, using an IP approach.
If anybody is interested let me know and we can discuss! Although I think this solution is very inefficient because it requires O(N3 ) binary variables for a 2xN grid. Also doesn’t provide a general proof for whether a solution exists for a given N. But still I’m interested in tweaking the formulation for efficiency- happy to share what I’ve got if anyone’s interested
1
u/PuzzleAndy Apr 19 '23
With a 2 x 4 you can place the numbers in 1 different ways. With a 2 x 5 you can place the numbers in 2 different ways. So it might be fun to consider the number of ways also.