我有这些圈子:
我想获取最大不相交圆的所有可能解决方案的列表。这是我要从节点A获得解决方案的说明。
因此,来自节点A的可能解决方案: 1 = [A,B,C],2 = [A,B,E],3 = [A,C,B],4 = [A,E,B] ..etc
我想将所有可能性存储在一个列表中,该列表将用于加权和选择最佳结果。但是,我仍在尝试创建所有可能性的列表。
我尝试在此处编写结构代码,但是我仍然对回溯和递归感到困惑。有人可以在这里帮忙吗?
# List of circle
# List of circle
list_of_circle = ['A','B','C','D','E']
# List of all possible solutions
result = []
# List of possible nodes
ways = []
for k in list_of_circle:
if len(list_of_circle)==0:
result.append(ways)
else:
ways.append[k]
list_of_circle.remove(k)
for j in list_of_circle:
if k.intersects(j):
list_of_circle.remove(j)
return result
答案 0 :(得分:0)
这是一个可能的解决方案(伪代码)。
def get_max_non_intersect(selected_circles, current_circle_idx, all_circles):
if current_circle_idx == len(all_circles): # final case
return selected_circles
# we recursively get the biggest selection of circles if the current circle is not selected
list_without_current_circle = get_max_non_intersect(selected_circles, current_circle_idx + 1, all_circles)
# now we check if we can add the current circle to the ones selected
current_intersects_selected = false
current_circle = all_circles[current_circle_idx]
for selected_circle in selected_circles:
if intersects(current_circle, selected_circle):
current_intersects_selected = true
break
if current_intersects_selected is true: # we cannot add the current circle
return list_without_current_circle
else: # we can add the current circle
list_with_current_circle = get_max_non_intersect(selected_circles + [current_circle], current_circle_idx + 1, all_circles)
return list_with_current_circle + list_without_current_circle