Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
安排回溯python_Python_Backtracking_N Queens - Fatal编程技术网

安排回溯python

安排回溯python,python,backtracking,n-queens,Python,Backtracking,N Queens,我很难找到如何在python上通过回溯生成安排,这是他们在大学里向我们提出的问题 一组n(n代码: 测试代码: def possible_solutions(neighbors): for solution in possible_solution(neighbors): print solution print '-' * 30 possible_solutions([1, 2, 3]) print '-' * 30 possible_solutions([1, 2

我很难找到如何在python上通过回溯生成安排,这是他们在大学里向我们提出的问题

一组n(n代码:

测试代码:

def possible_solutions(neighbors):
    for solution in possible_solution(neighbors):
        print solution

print '-' * 30
possible_solutions([1, 2, 3])

print '-' * 30
possible_solutions([1, 2, 3, 4])

print '-' * 30
possible_solutions([1, 2, 3, 4, 5])
结果:

------------------------------
------------------------------
[2, 4, 1, 3]
[3, 1, 4, 2]
------------------------------
[1, 3, 5, 2, 4]
[1, 4, 2, 5, 3]
[2, 4, 1, 3, 5]
[2, 4, 1, 5, 3]
[2, 5, 3, 1, 4]
[3, 1, 4, 2, 5]
[3, 1, 5, 2, 4]
[3, 5, 1, 4, 2]
[3, 5, 2, 4, 1]
[4, 1, 3, 5, 2]
[4, 2, 5, 1, 3]
[4, 2, 5, 3, 1]
[5, 2, 4, 1, 3]
[5, 3, 1, 4, 2]
代码:

测试代码:

def possible_solutions(neighbors):
    for solution in possible_solution(neighbors):
        print solution

print '-' * 30
possible_solutions([1, 2, 3])

print '-' * 30
possible_solutions([1, 2, 3, 4])

print '-' * 30
possible_solutions([1, 2, 3, 4, 5])
结果:

------------------------------
------------------------------
[2, 4, 1, 3]
[3, 1, 4, 2]
------------------------------
[1, 3, 5, 2, 4]
[1, 4, 2, 5, 3]
[2, 4, 1, 3, 5]
[2, 4, 1, 5, 3]
[2, 5, 3, 1, 4]
[3, 1, 4, 2, 5]
[3, 1, 5, 2, 4]
[3, 5, 1, 4, 2]
[3, 5, 2, 4, 1]
[4, 1, 3, 5, 2]
[4, 2, 5, 1, 3]
[4, 2, 5, 3, 1]
[5, 2, 4, 1, 3]
[5, 3, 1, 4, 2]

作业?1 3 4 2是不可能的,因为3和4是相邻的。作业?1 3 4 2是不可能的,因为3和4是相邻的。这看起来是正确的,但不是像我以前做的那样真正的修改代码,我知道python是一种高端语言,从我被告知的,从我读到的我认为cond函数是你放置所有条件的地方…@JackRobinson:这是我用python编写解决方案的方式。即使我想使用你的框架,我也需要看看
回溯类是如何工作的,以及它是如何使用的。这看起来不错,但并不像我以前那样是一个真正的修改代码,我知道python是一种高端语言,从我被告知的,从我读到的内容来看,我认为函数是您放置所有条件的地方…@JackRobinson:这就是我用python编写解决方案的方式。即使我想使用您的框架,我也需要了解
回溯类是如何工作的,以及它是如何使用的。(1)在建议的解决方案中使用整数标记最后一个值是有问题的,例如:
打印列表(打印列表)(set([-1,0,1,2]),-1))
。您最终必须根据传入的数据更改此标志的值。(2)您可以传入一个新副本,而不是从集合中移除并重新添加:
for soln in chairs(剩余的-set([r]),r):
。我不知道为什么这不是一个问题,但是修改正在循环的iterable是危险的。(3)建议:
如果r不在(prev+1,prev-1):
谢谢Hugh,我同意你的观点(和+1'd)。我找到了一种方法来完成整个事情,它避免了你提到的问题,也避免了内存分配(除非找到解决方案),并删除一些O(n^2)集合和列表结构。(1)在建议的解决方案中使用整数标记最后一个值是有问题的,例如:
打印列表(椅子(集合([-1,0,1,2]),-1))
。最后必须根据传入的数据更改此标志的值。(2)与其从集合中删除并添加回来,您可以传入一个新副本:
用于soln in chairs(剩余集合([r]),r):
。我不知道为什么这不是一个问题,但修改正在循环的iterable是危险的。(3)建议:
如果r不在(prev+1,prev-1):
谢谢Hugh,我同意您的观点(和+1'd)。我找到了一种方法来完成整个过程,它避免了您提到的问题,也避免了内存分配(找到解决方案时除外),并去掉了一些O(n^2)集合和列表结构。
------------------------------
------------------------------
[2, 4, 1, 3]
[3, 1, 4, 2]
------------------------------
[1, 3, 5, 2, 4]
[1, 4, 2, 5, 3]
[2, 4, 1, 3, 5]
[2, 4, 1, 5, 3]
[2, 5, 3, 1, 4]
[3, 1, 4, 2, 5]
[3, 1, 5, 2, 4]
[3, 5, 1, 4, 2]
[3, 5, 2, 4, 1]
[4, 1, 3, 5, 2]
[4, 2, 5, 1, 3]
[4, 2, 5, 3, 1]
[5, 2, 4, 1, 3]
[5, 3, 1, 4, 2]