最长超立方体路径


18

挑战

系统会为您提供两个相同长度的不同位串。(例如000111。)您的目标是找到一条到另一条的路径,使得:

  • 在每一步,你只改变一位(你可以从去000到任何的001010100)。
  • 您不能访问同一位字符串两次。
  • 在这些约束下,路径应尽可能长。

例如,从000111,我们可以走这条路

000, 001, 011, 010, 110, 100, 101, 111

它访问长度为3的所有8位字符串,因此它必须尽可能长。

规则

  • 有标准漏洞。
  • 您可以将输入作为两个零和一的字符串,或者作为两个零和一的数组,或者作为两个布尔值的数组。
  • 您可能无法将输入取为带有正确二进制表示形式的两个整数(写000111as 0和as 7无效)。
  • 如果需要,可以将位串的长度作为输入。
  • 您的程序可以通过以下方式输出路径:打印一次访问的位串,或返回访问的位串的数组(每个都与输入格式相同)。
  • 您的输出应包括路径的起点和终点(即您的输入)。
  • 这是,以字节为单位的最短代码获胜。

例子

0 1 -> 0, 1
10 01 -> 10, 00, 01 or 10, 11, 01
000 111 -> any of the following:

   000, 100, 110, 010, 011, 001, 101, 111

   000, 100, 101, 001, 011, 010, 110, 111

   000, 010, 110, 100, 101, 001, 011, 111

   000, 010, 011, 001, 101, 100, 110, 111

   000, 001, 101, 100, 110, 010, 011, 111

   000, 001, 011, 010, 110, 100, 101, 111

1001 1100 -> 1001, 0001, 0000, 0010, 0011, 0111, 0101, 0100, 0110, 1110, 1010, 1011, 1111, 1101, 1100 (other paths exist)

1
我们还可以采用布尔值而不是1和0吗?
瑕疵

@flawr当然可以。
Misha Lavrov

可以假设我们不会收到两个相等的位字符串(或者如果可以的话,我们可以做任何事情)?
乔纳森·艾伦

1
@JonathanAllan是的,让我们假设位串不相等。
Misha Lavrov

Answers:


6

外壳27 26 24字节

→foΛεẊδṁ≠ÖLm↓≠⁰←ġ→PΠmṠe¬

蛮力,太慢了。 在线尝试!

说明

稻壳从右到左自然阅读。

←ġ→PΠmṠe¬  Hypercube sequences ending in second input, say y=[1,1,0]
     mṠe¬  Pair each element with its negation: [[0,1],[0,1],[1,0]]
    Π      Cartesian product: [[0,0,1],[1,0,1],..,[1,1,0]]
   P       Permutations.
 ġ→        Group by last element
←          and take first group.
           The permutations are ordered so that those with last element y come first,
           so they are grouped together and returned here.

ÖLm↓≠⁰  Find first input.
  m     For each permutation,
   ↓≠⁰  drop all elements before the first input.
ÖL      Sort by length.

foΛεẊδṁ≠  Check path condition.
fo        Keep those lists that satisfy:
    Ẋ      For each adjacent pair (e.g. [0,1,0] and [1,1,0]),
      ṁ    take sum of
       ≠   absolute differences
     δ     of corresponding elements: 1+0+0 gives 1.
  Λε       Each value is at most 1.

→  Finally, return last element (which has greatest length).

4

Mathematica,108个字节

a=#~FromDigits~2+1&;Last@PadLeft[IntegerDigits[#-1,2]&/@FindPath[HypercubeGraph@Length@#,a@#,a@#2,∞,All]]&

输入:

[{0, 0, 0, 0}, {1, 1, 1, 1}]

输出:

{{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 1}, {0, 0, 1, 0}, {0, 1, 1, 0},
 {0, 1, 0, 0}, {0, 1, 0, 1}, {1, 1, 0, 1}, {1, 0, 0, 1}, {1, 0, 0, 0},
 {1, 1, 0, 0}, {1, 1, 1, 0}, {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 1, 1}}

3

Mathematica,175个字节

不错的第一个问题!

(m=#;n=#2;Last@SortBy[(S=Select)[S[Rest@Flatten[Permutations/@Subsets[Tuples[{0,1},(L=Length)@m]],1],First@#==m&&Last@#==n&],Union[EditDistance@@@Partition[#,2,1]]=={1}&],L])&   


输入值

[{0,0,0},{1,1,1}]


3

Haskell中212个 207字节

这可能太长了,但现在终于可以使用了。(感谢@Lynn的笛卡尔积技巧!)Thansk @nimi为-5字节!

import Data.List
b%l=[l++[x|b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v]]|x<-mapM id$[0>1..]<$b]
b!a|f<-nub.concat.((b%)<$>)=snd$maximum$map(length>>=(,))$filter((==b).last)$until(f>>=(==))f[[a]]

在线尝试!

说明:

b%l -- helper function:
    -- given a path l (that should end in b) this generates all possible extensions
    -- of l (if not possible also l itself) 
            x<-mapM id$[0>1..]<$b -- generate all possible vertices of the hypercube
             -- and check the criteria
           b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v] 
             -- extend if possible
    [l++[x|  ...                                                   ]| ... ]
b!a| -- actual function: 
     -- first define a helper function:
    f<-nub.concat.((b%)<$>)
     -- begin with the vertex a and apply the function from above repeatedly
     -- until you cannot make the path any longer without violating the
     -- criteria 
                                                                             until(f>>=(==))f[[a]]
     -- only take the paths that actually end in b          
                                                          filter((==b).last)$
     -- and find the one with the maximum length    
                           =snd$maximum$map(length>>=(,))$    

x<-mapM id$[1>0,1<0]<$b
nimi

...您需要[True,False]吗?如果[False,True]还可以,则可以使用[0>1..]
nimi

哦,太好了,谢谢,我不知道那Bool是什么Enum,我忘记了<$可用的功能(首先尝试过*>,但未在Prelude中使用)!
瑕疵的

3

Mathematica 116114字节

由于Misha Lavrov节省了几个字节。

Last@FindPath[Graph[Rule@@@Cases[Tuples[Tuples[{0,1},{l=Length@#}],{2}],x_/;Count[Plus@@x,1]==1]],##,{1,2^l},Alll]&

输入(8个尺寸)

[{1,0,0,1,0,0,0,1},{1,1,0,0,0,0,1,1}]//AbsoluteTiming

输出(1.82秒后,长度= 254)

{1.82393, {{1, 0, 0, 1, 0, 0, 0, 1}, {0, 0, 0, 1, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 0}, {0, 0,0, 0, 0, 0, 1, 1}, {0, 0, 0, 0, 0, 1, 1, 1}, {0, 0, 0, 0, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1, 1, 0}, {0, 0, 0, 0,1, 1, 1,0}, {0, 0, 0, 0, 1, 0, 1, 0}, {0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 1}, {0, 0, 0,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}, {1, 1, 1, 0, 1, 1, 0, 1}, {1, 1, 1, 0, 0, 1, 0, 1}, {1, 1, 1, 0, 0, 0, 0, 1}, {1, 1, 1, 0, 0, 0, 0, 0}, {1, 1, 1, 0,0, 0, 1, 0}, {1, 1, 1, 0, 0, 1, 1, 0}, {1, 1, 1, 0, 0, 1, 0, 0}, {1, 1, 1, 0, 1, 1, 0, 0}, {1, 1, 1, 0, 1, 0, 0, 0}, {1, 1, 1, 0,1, 0, 0, 1}, {1, 1, 1, 0, 1, 0, 1, 1}, {1, 1, 1, 0, 1, 0, 1, 0}, {1, 1, 1, 0, 1, 1, 1, 0}, {1, 1, 1, 0, 1, 1, 1, 1}, {1, 1, 1, 0,0, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 1, 0}, {1, 1, 1, 1, 0, 0, 1, 0}, {1, 1, 1, 1, 0, 0, 0, 0}, {1, 1, 1, 1,0, 0, 0, 1}, {1, 1, 1, 1, 1, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 0}, {1, 1, 1, 1,1, 0, 1, 0}, {1, 1, 1, 1, 1, 0, 0, 0}, {1, 0, 1, 1, 1, 0, 0, 0}, {1, 0, 1, 1, 1, 0, 0, 1}, {1, 0, 1, 1, 1, 0, 1, 1}, {1, 1, 1, 1,1, 0, 1, 1}, {1, 1, 1, 1, 0, 0, 1, 1}, {1, 1, 1, 0, 0, 0, 1, 1}, {1, 1, 0, 0, 0, 0, 1, 1}}}

Tuples[{0,1},{l=Length@#}],{2}]&生成数字0 ... 8作为二进制列表。

外部Tuples...{2}产生这些二进制数的所有有序对。

Plus@@x 对每对进行求和,生成三元组0、1。

Cases....Count[Plus@@x, 1]==1 返回所有包含单个1的总和。当两个原始二进制数通过边连接时,就会出现这些总和。

Rules 连接图的顶点,每个顶点是一个二进制数。

Graph 创建与所述顶点和边缘相对应的图形。

FindPath 查找给定数目的最多2 ^ n条连接顶点a和顶点b的路径。

Last 采取这些路径中最长的。


对于三个维度,图形可以在一个平面中表示,如下所示:

平面图

对于输入,{0,0,0}, {1,1,1}输出以下内容:

{{{0, 0, 0}, {0, 0, 1}, {0, 1, 1}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}, {1, 0, 1}, {1, 1, 1}}}

可以在上图中找到此路径。

也可以将其视为3空间中的以下路径,其中每个顶点对应于一个点{x,y,z}。{0,0,0}表示原点,{1,1,1}表示单位立方体中的“相对”点。

因此,求解路径对应于沿单位立方体的边的遍历。在这种情况下,路径是哈密顿量:它一次访问每个顶点(即不交叉且不省略任何顶点)。

44


有简单的原因为什么从a到b的2 ^ n条路径足以使最长的一条路径成为最长的路径?
Misha Lavrov

@Misha,一个很好的问题。
DavidC

这是一种思考方式。最长的路径(哈密顿路径)比拐角数少一。(我们计算的是路径上的边数。)角数为2 ^ n。因此,最大路径长度将为2 ^ n-1。
DavidC

我同意最大路径长度总是访问2 ^ n个顶点(如果它是哈密顿量)或2 ^ n-1个顶点(如果由于奇偶校验而不可能使用哈密顿量)。这与我的问题不同,那就是:为什么生成2 ^(n + 2)(我猜2 ^ n是错误的数字)不同的路径(其中一些路径可能很短)保证了最长的路径是所有不同路径中最长的。
Misha Lavrov

换句话说,为什么2^(l+2)在您的代码中?
Misha Lavrov

3

Haskell141123字节

c(a:b)=(1-a:b):map(a:)(c b)
c _=[]
q#z=[z]:[z:s|w<-c z,notElem w q,s<-(w:q)#w]
x!y=snd$maximum[(p*>x,p)|p<-[x]#x,last p==y]

使用整数列表。 在线尝试!

说明

主要功能为!,辅助功能为#c。给定一个位列表,c给出了所有可能的方式来翻转其中之一,例如[0,1,1] -> [[1,1,1],[0,0,1],[0,1,0]]

c(a:b)=        -- c on nonempty list with head a and tail b is
 (1-a:b):      -- the list with negated a tacked to b, then
 map(a:)(c b)  -- c applied recursively to b, with a tacked to each of the results.
c _=[]         -- c on empty list gives an empty list.

该函数#获取一个列表列表(“内存”)和一个列表(“初始位串”)。它构造了所有以初始元素开头的超立方体路径,仅包含不同的位串,并且不踩踏内存中的字符串。

q#z=            -- # on memory q and initial string z is
 [z]:           -- the singleton path [z], and
 [z:s|          -- z tacked to each path s, where
  w<-c z,       -- w is obtained by flipping a bit of z,
  notElem w q,  -- w is not in the memory, and
  s<-(w:q)#w]   -- s is a path starting from w that avoids w and all elements of q.

主要功能!将它们联系在一起。我在这里使用的一个技巧是p*>xx重复length p几次)而不是length p。由于较长的重复x序列在列表的自然顺序中排在后面,因此maximum在两种情况下都选择最长的路径,这是因为对的第一个坐标在第二个坐标之前进行比较。

x!y=          -- ! on inputs x and y is
 snd$maximum  -- the second element of the maximal pair in
 [(p*>x,p)|   -- the list of pairs (p*>x,p), where
  p<-[x]#x,   -- p is a path starting from x that avoids stepping on x, and
  last p==y]  -- p ends in y.

2

果冻 25  27 字节

+2个字节来修正我的高尔夫错误:(希望我能找到更短的方法。

ṫi¥³ḣi
L2ṗŒ!瀵ạ2\S€ỊẠ×LµÞṪ

一个完整的程序,使用12*作为位串。参数是fromto。该程序将打印相同列表的列表。

* 0并且1可以代替使用,但要花费一个字节(L2ṗ和之间增加Œ!ç€...)。

在线尝试!

怎么样?

更新中...

ṫi¥³ḣi - Link 1, getSlice: list of lists, bitstrings; list, toBitstring
   ³   - get 3rd command line argument (fromBitstring)
  ¥    - last two links as a dyad:
 i     -   index (of fromBitstring in bitstrings)
ṫ      -   tail (bitstrings) from (that) index
     i - index (of toBitstring in that result)
    ḣ  - head to (that) index

L2ṗŒ!瀵ạ2\S€ỊẠ×LµÞṪ - Main link: list, fromBitstring; list, toBitstring
L                    - length (of fromBitstring)
 2                   - literal two
  ṗ                  - Cartesian power (of implicit range(2)=[1,2] with L(fromBitstring))
                     - ...i.e. all unique bitstrings of the required length (using [1,2])
   Œ!                - all permutations (of that list)
     ç€              - call the last link (1) as a dyad (i.e. f(that, toBitstring))
       µ         µÞ  - sort by the monadic function:
         2\          -   2-wise reduce with:
        ạ            -     absolute difference
           S€        -   sum €ach
             Ị       -   insignificant (vectorises) (abs(z)<=1 - for our purposes it's really just used for z==1 since only positive integers are possible)
              Ạ      -   all truthy? (1 if so 0 otherwise)
                L    -   length
               ×     -   multiply
                   Ṫ - tail (the last one is one of the maximal results)
                     - implicit print

Jelly的工作方式对我来说还是个谜,但是当我在网上尝试时,输入[1,1][2,2]产生的结果[[1, 1], [2, 1], [1, 2], [2, 2]]却是无效的。
Misha Lavrov

嗯,我一定做错了-看...
Jonathan Allan

可以通过将我的一个高尔夫球恢复2个字节来解决。
乔纳森·艾伦
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.