构造正交对角希腊拉丁广场


11

考虑一个由Nx个N唯一元素组成的网格。每个元素都有一个字母(从A到第Nth个字母,包括端点)和一个数字(从1到N,包括端点)。因此,每个数字/字母对恰好在网格中一次。

您的工作是安排一个网格,以便:

每行,每列和对角线(包括换行)仅包含每个字母和数字一次。

通过包装,我的意思是

* * * # *
* * # * * 
* # * * *
# * * * *
* * * * #

是对角线,以及碰到边缘的所有类似对角线。

一个示例5x5网格是:

A1 B2 C3 D4 E5
C4 D5 E1 A2 B3
E2 A3 B4 C5 D1
B5 C1 D2 E3 A4
D3 E4 A5 B1 C2

您的任务是编写一个接受数字的程序N,并如上所述打印Nx N网格。您的程序应该适用于任何程序0 < N <= 26。如果无法使用特定的网格,则应打印Impossible

不允许对答案进行硬编码N。如果程序以任何方式(根据我的判断)计算网格N > 26(或计算失败),则该程序将进行硬编码。(这是为了防止进行预先计算,包括预先计算的无效网格或给定网格的偏移量)。

这是一个最快的代码问题,您的分数是N在我的计算机上所有可能的程序上运行所花费的时间总和。在您的回答中,请让您的程序在所有程序上运行N(所以我只需要定时一次)。如果没有程序可以在60秒内计算出网格,那么赢家就是可以N在60秒内计算出最大网格的答案。如果多个程序具有相同的最大值N,那么决胜局是最快的解决方案。

(我有一台功能强大的Windows 8计算机,必须免费提供所需的任何编译器或解释器)。


对于某些技术来说,您的计算机是Windows而不是Linux可能会使您感到困扰。
orlp 2015年

+1,但是鉴于您对示例的分析似乎给出了一种非常快速的算法,我想知道您是否真的能够测量速度。我们可以编写一个返回字符串的函数吗?因为我相信API调用完成实际打印所花费的时间将比计算所需的时间更长。
Level River St

@steveverrill是的,出于计时目的,返回字符串是可接受的。
内森·美林

字母和数字的目的是什么。每个数字仅出现在每个字母旁边一次,还是1总是出现在A旁边,2总是出现在B旁边,...?
2015年

@Jakube是的。每个元素必须是唯一的,这意味着网格中的每个数字/字母对都必须是唯一的。
弥敦道·美林

Answers:


5

Python 3

letters = []
numbers = []
for n in range(1,27): 
    if n%2==0 or n%3==0:
        offsets=False
    else:
        offsets = [x for x in range(0,n,2)]
        offsets.extend([x for x in range(1,n,2)])
    letters.append(chr(96+n))
    numbers.append(n)
    if offsets :
        for y in range(n):
            for x in range(n):
                let=letters[(x+offsets[y])%n]
                num=numbers[(offsets[y]-x)%n]
                print (let+str(num), end= "  " if num<10 else " ")
            print("\n")     
    else: 
        print("Impossible\n")

这个怎么运作?

天真的实现方式是查看NxN网格中字母和数字的所有可能排列,并寻找一个也是正交对角拉丁方形(ODLS)的对象(因此,对于某些对象,它仅需要遍历所有配置,无法返回)。由于荒谬的时间复杂性,这种算法不适合该挑战。因此,在我的实现中使用的ODLS构造有三个主要的简化和理由(部分证明以及对其工作原理的见解):

第一个概念是,我们只需要生成一个有效的对角拉丁方形(一个NxN网格,这样,每行,每一列,包裹的对角线就将一组N个不同元素中的每个元素恰好包含一次)就可以了。字母。如果我们可以构造这样的对角拉丁方形(DLS),则可以使用DLS通过适当的元素交换和翻转来构造ODLS。理由:

Let us first look at an example using the example grid
a1 b2 c3 d4 e5
c4 d5 e1 a2 b3
e2 a3 b4 c5 d1
b5 c1 d2 e3 a4
d3 e4 a5 b1 c2
Every ODLS can be separated into two DLS (by definition), so
we can separate the grid above into two DLS, one containing letters, the other - numbers
a b c d e
c d e a b
e a b c d
b c d e a
d e a b c
and
1 2 3 4 5 
4 5 1 2 3
2 3 4 5 1
5 1 2 3 4 
3 4 5 1 2
If we transform the number DLS by the mapping 1-->e, 2-->d, 3-->c, 4-->b, 5-->a,
1 2 3 4 5 --> e d c b a
4 5 1 2 3 --> b a e d c
2 3 4 5 1 --> d c b a e
5 1 2 3 4 --> a e d c b
3 4 5 1 2 --> c b a e d
Now if we put the transformed number grid next to the original letter grid,
Original  | Transformed
a b c d e | e d c b a
c d e a b | b a e d c
e a b c d | d c b a e
b c d e a | a e d c b
d e a b c | c b a e d
It can be clearly seen that the number grid is a horizontal flip of
the letter grid withminor letter to number substitutions.
Now this works because flipping guarantees that no two pairs occur more than once,
and each DLS  satisfies the requirements of the ODLS.

第二个简化是这样一个概念:如果我们找到一个元素的适当配置(SC)(一个NxN网格,使得每一行,每一列,对角线包裹的元素恰好包含一次该元素),则可以通过替换元素来构造DLS并转移SC。理由:

If "_" is an empty space and "a" the element then a valid SC of a 7x7 grid is
a _ _ _ _ _ _
_ _ a _ _ _ _
_ _ _ _ a _ _
_ _ _ _ _ _ a
_ a _ _ _ _ _ 
_ _ _ a _ _ _
_ _ _ _ _ a _
or
a _ _ _ _ _ _
_ _ _ a _ _ _
_ _ _ _ _ _ a
_ _ a _ _ _ _
_ _ _ _ _ a _ 
_ a _ _ _ _ _
_ _ _ _ a _ _
(the second one can actually be obtained from the first one via rotation)
now say we took the second SC, shifted it one unit to the right and 
replaced all "a" with "b"
a _ _ _ _ _ _       _ a _ _ _ _ _       _ b _ _ _ _ _
_ _ _ a _ _ _       _ _ _ _ a _ _       _ _ _ _ b _ _
_ _ _ _ _ _ a       a _ _ _ _ _ _       b _ _ _ _ _ _
_ _ a _ _ _ _  -->  _ _ _ a _ _ _  -->  _ _ _ b _ _ _
_ _ _ _ _ a _       _ _ _ _ _ _ a       _ _ _ _ _ _ b
_ a _ _ _ _ _       _ _ a _ _ _ _       _ _ b _ _ _ _
_ _ _ _ a _ _       _ _ _ _ _ a _       _ _ _ _ _ b _
Now if we overlaid the SC of "a" with the SC of "b" we get
a b _ _ _ _ _
_ _ _ a b _ _
b _ _ _ _ _ a
_ _ a b _ _ _
_ _ _ _ _ a b 
_ a b _ _ _ _
_ _ _ _ a b _
If we repeated these steps for the other five letters, we would arrive at a DLS
a b c d e f g
e f g a b c d
b c d e f g a
f g a b c d e
c d e f g a b 
g a b c d e f
d e f g a b c
This is a DLS, since each SC follows the general requirements of a DLS 
and shifting ensured that each element has its own cell.
Another thing to note is that each row contains the string "abcdefg" that is offset 
by some cells. This leads to another simplification: we only need to find the 
offsets of the string in every row and we are finished.

最后一个简化如下-可以构造除N = 2或N = 3以外的所有素数N的DLS,如果可以将N分解为两个可以构造适当DLS的数,则该N的DLS可以被建造。我猜想相反也成立。(换句话说,我们只能为不能被2或3整除的N构造DLS)

Pretty obvious why 2x2 or 3x3 cant be made. For any other prime this can be done
by assigning a each consecutive row a shift that is by two bigger than the previous, 
for N=5 and N=7 this looks like (with elements other than "a" ommited)
N=5
a _ _ _ _ offset = 0
_ _ a _ _ offset = 2
_ _ _ _ a offset = 4
_ a _ _ _ offset = 6 = 1 (mod 5)
_ _ _ a _ offset = 8 = 3 (mod 5)
N=7
a _ _ _ _ _ _ offset = 0
_ _ a _ _ _ _ offset = 2
_ _ _ _ a _ _ offset = 4
_ _ _ _ _ _ a offset = 6
_ a _ _ _ _ _ offset = 8 = 1 (mod 7)
_ _ _ a _ _ _ offset = 10 = 3 (mod 7)
_ _ _ _ _ a _ offset = 12 = 5 (mod 7
(Why this works on all prime N (actually all N that are not divisible
by 3 or 2) can probably be proven via some kind of induction but i will
omit that, this is just what my code uses and it works)
Now, the first composite number that is not
divisible by 2 or 3 is 25 (it also occurs in the range our program must test)
Let A denote the DLS of N = 5
a b c d e 
d e a b c 
b c d e a 
e a b c d 
c d e a b
Let F be the DLS A where each letter is substituted by the letter five postions after it 
a-->f, b-->g etc. So F is 
f g h i j 
j e f g h 
g h i j f 
j f g h i 
h i j f g
Let K be the DLS a where each letter is substituted by the letter ten postions after it
a-->k, b--> l etc.
Let P be defined likewise (so a-->p, b-->q etc)
Let U be defined likewise (so a-->u, b-->v etc)
Now, since the DLS A could be constructed, then by substituting a --> A, b--> F etc.
we get a DLS of N=5*5 (A has five rows and five columns and each is filled with a 
grid of five rows and five columns)
A F K P U
P U A F K
F K P U A
U A F K P
K P U A F
Now since smaller DLS in the big DLS satisfies the 
conditions of a DLS and the big one also satisfies the DLS conditions,
then the resulting grid is also a DLS 

在此处输入代码

我的意思是较小的图片-较大的DLS

Now this kind of thing works for all constructible N and can be proven similiarly.

I have a strong sense that the converse (if some N isnt constructible
(2 and 3) then no multiple of that N is constructible) is also true but have a hard time 
proving it (test data up to N=30 (took a veeery long time to calculate) confirm it though)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.