Solving N-Queens Problem… How far can we go?

后端 未结 9 949
闹比i
闹比i 2020-12-09 03:23

The N-Queens Problem:

This problem states that given a chess board of size N by N, find the different permutations in which N queens can be placed on the board with

9条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 04:10

    a short solution presented by raymond hettinger at pycon: easy ai in python

    #!/usr/bin/env python
    from itertools import permutations
    n = 12
    cols = range(n)
    for vec in permutations(cols):
        if (n == len(set(vec[i] + i for i in cols))
              == len(set(vec[i] - i for i in cols))):
            print vec
    

    computing all permutations is not scalable, though (O(n!))

提交回复
热议问题