讨论/技术交流/3个K,2个L,1个G,排列顺序随意,如何输出一个枚举所有排列的序列?/
3个K,2个L,1个G,排列顺序随意,如何输出一个枚举所有排列的序列?

如题

共 1 个回复

回溯法:

chars = 'KKKLLG'
visited = [False] * 6
repeated = {}

def dfs(n: int, s: str) -> None:
    if n == 6:
        v = repeated.get(s, False)
        if not v:
            repeated[s] = True
            print(s)
    else:
        for i in range(6):
            if visited[i]:
                continue
            else:
                visited[i] = True
                dfs(n + 1, s + chars[i])
                visited[i] = False


if __name__ == '__main__':
    dfs(0, '')