English 中文(简体)
Grid Permutation Algorithm - Persistent Row Order
原标题:Grid Permutation Algorithm - Fixed Row Order

Imagine a 3x3 grid:

[A, B, %]
[C, %, D]
[E, F, G]

<>%在空位/空位中位。

浏览量可以像灯塔那样,这样,第一行组合的变动可能是:

[A, B, %] or [A, %, B] or [%, A, B]

同样,第二行也是如此。 第三行没有空档,因此无法改变。

鉴于每一行之间可能相互交错,我正试图生产所有可能的电网。

产出应产生以下电网:

[A, B, %]    [A, B, %]    [A, B, %]
[C, D, %]    [C, %, D]    [%, C, D]
[E, F, G]    [E, F, G]    [E, F, G]

[A, %, B]    [A, %, B]    [A, %, B]
[C, D, %]    [C, %, D]    [%, C, D]
[E, F, G]    [E, F, G]    [E, F, G]

[%, A, B]    [%, A, B]    [%, A, B]
[C, D, %]    [C, %, D]    [%, C, D]
[E, F, G]    [E, F, G]    [E, F, G]

我尝试了一种办法,通过每一行寻找,改变左翼和右边,然后产生新的电网,并重新开垦。 我把所有电网都保留在一套电网中,确保我只生产已经经过审查的避开无限期再入侵的位置。

然而,我的算法似乎非常低效率(每mut1s!) 并且不只看一.。 我很想知道,这样做是否有雄辩的方法? 尤其是 p。

我有一些模糊的想法,但我确信,这样做是短暂和简单,我 over。

<>光线> 3x3只是一个例子。 争.可能具有任何规模,而且确实是混血管。 例如:

[A, %, C]
[D, E, %, G]
[H, I]

也是有效的网格。

<><><>><>>>> 这些信件必须维持命令。 例如[A. %, B]! [B, %,A][B, A, %]

最佳回答

首先,你们必须获得每一行所期望的平衡。 然后,你计算所有线的交叉产品。

一条线的变动可以简单地计算,其方法是将信函[A,B,%]和不同的起始指数:

import itertools
# Example: line = [ A , B , % ]
def line_permutations(line):
   if  %  not in line:
       return [line]
   line.remove( % ) # use copy.copy if you don t want to modify your matrix here
   return (line[:i] + [ % ] + line[i:] for i in range(len(line) + 1))

跨产品最容易使用惯性设备。 产品

matrix = [[ A , B , % ], [ C ,  % ,  D ], [ E ,  F ,  G ]]
permutations = itertools.product(*[line_permutations(line) for line in matrix])
for p in permutations:
    print(p)

这一解决办法是最佳的记忆,是万国邮联的要求,因为从未重新计算过。

例产出:

([ % ,  A ,  B ], [ % ,  C ,  D ], [ E ,  F ,  G ])
([ % ,  A ,  B ], [ C ,  % ,  D ], [ E ,  F ,  G ])
([ % ,  A ,  B ], [ C ,  D ,  % ], [ E ,  F ,  G ])
([ A ,  % ,  B ], [ % ,  C ,  D ], [ E ,  F ,  G ])
([ A ,  % ,  B ], [ C ,  % ,  D ], [ E ,  F ,  G ])
([ A ,  % ,  B ], [ C ,  D ,  % ], [ E ,  F ,  G ])
([ A ,  B ,  % ], [ % ,  C ,  D ], [ E ,  F ,  G ])
([ A ,  B ,  % ], [ C ,  % ,  D ], [ E ,  F ,  G ])
([ A ,  B ,  % ], [ C ,  D ,  % ], [ E ,  F ,  G ])
问题回答

界定一个称为周期的职能

>>> def cycle(lst):
    while True:
        lst=lst[1:]+lst[0:1] if  %  in lst else lst
        yield lst
  • Given an iterator, this will generate and return a cyclic left shift.
  • Now you have to pass each of the rows in the grid to the cycle generator for the total iteration matching the length of the row
  • Now use itertools.product to find all combinations of the generated row cyclic combinations.
  • In case there is no empty slot, no cycle permutation is generated

最后结果如下:

>>> for g in list(itertools.product(*[[x for (x,y) in zip(cycle(row),
           range(0,len(row) if  %  in row else 1))] for row in grid])):
    for r in g:
        print r
    print "="*10

对您的格里德来说,这将产生结果。

[ B ,  % ,  A ]
[ % ,  D ,  C ]
[ E ,  F ,  G ]
===============
[ B ,  % ,  A ]
[ D ,  C ,  % ]
[ E ,  F ,  G ]
===============
[ B ,  % ,  A ]
[ C ,  % ,  D ]
[ E ,  F ,  G ]
===============
[ % ,  A ,  B ]
[ % ,  D ,  C ]
[ E ,  F ,  G ]
===============
[ % ,  A ,  B ]
[ D ,  C ,  % ]
[ E ,  F ,  G ]
===============
[ % ,  A ,  B ]
[ C ,  % ,  D ]
[ E ,  F ,  G ]
===============
[ A ,  B ,  % ]
[ % ,  D ,  C ]
[ E ,  F ,  G ]
===============
[ A ,  B ,  % ]
[ D ,  C ,  % ]
[ E ,  F ,  G ]
===============
[ A ,  B ,  % ]
[ C ,  % ,  D ]
[ E ,  F ,  G ]
===============

阴残割:

g=[[ A ,  B ,  % ],
[ C ,  % ,  D ],
[ E ,  F ,  G ]]

from collections import deque
from itertools import product

def rotations(it):
    d = deque(it,len(it))
    for i in xrange(len(it)):
         d.rotate(1)
         yield list(d)

for i in product(*[rotations(x) if  %  in x else [x] for x in g]):
    print i

说明:

([ % ,  A ,  B ], [ D ,  C ,  % ], [ E ,  F ,  G ])
([ % ,  A ,  B ], [ % ,  D ,  C ], [ E ,  F ,  G ])
([ % ,  A ,  B ], [ C ,  % ,  D ], [ E ,  F ,  G ])
([ B ,  % ,  A ], [ D ,  C ,  % ], [ E ,  F ,  G ])
([ B ,  % ,  A ], [ % ,  D ,  C ], [ E ,  F ,  G ])
([ B ,  % ,  A ], [ C ,  % ,  D ], [ E ,  F ,  G ])
([ A ,  B ,  % ], [ D ,  C ,  % ], [ E ,  F ,  G ])
([ A ,  B ,  % ], [ % ,  D ,  C ], [ E ,  F ,  G ])
([ A ,  B ,  % ], [ C ,  % ,  D ], [ E ,  F ,  G ])

这里就是这样。 请注意,这种做法在一方不太干净,但允许你只一次计算第10(matrix [0]-1号的改动,并将其作为产出的模板。

from itertools import permutations,product

def charPermutation(matrix):
    permutation_list=list(permutations(["%%"]+["%s"]*(len(matrix[0])-1))) #Compute once, use infinitely
    perms={i:[] for i in range(len(matrix))}
    for it,line in enumerate(matrix):
        chars=list(set(line)-set(["%"]))
        if sorted(line)==sorted(chars):
            perms[it]+=["".join([str(i) for i in line])]
        else:
            for p in permutation_list:
                perms[it]+=["".join(["".join(p) % tuple(chars)])]
        perms[it]=list(set(perms[it]))
    return product(*[[list(k) for k in i] for i in perms.values()]) 

g=[
   ["A", "B", "%"],
   ["C", "%", "D"],
   ["E", "F", "G"]]

for x in charPermutation(g):
    print [i for i in x]

[[ A ,  % ,  B ], [ C ,  D ,  % ], [ E ,  F ,  G ]]
[[ A ,  % ,  B ], [ % ,  C ,  D ], [ E ,  F ,  G ]]
[[ A ,  % ,  B ], [ C ,  % ,  D ], [ E ,  F ,  G ]]
[[ % ,  A ,  B ], [ C ,  D ,  % ], [ E ,  F ,  G ]]
[[ % ,  A ,  B ], [ % ,  C ,  D ], [ E ,  F ,  G ]]
[[ % ,  A ,  B ], [ C ,  % ,  D ], [ E ,  F ,  G ]]
[[ A ,  B ,  % ], [ C ,  D ,  % ], [ E ,  F ,  G ]]
[[ A ,  B ,  % ], [ % ,  C ,  D ], [ E ,  F ,  G ]]
[[ A ,  B ,  % ], [ C ,  % ,  D ], [ E ,  F ,  G ]]




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签