て行くことはありません。バックトラックは、私は問題を解決しようとしていたすべての可能性
私は質問の名前といくつかのポイントを持っている辞書を持っています。たとえばQs = {"question1": 13, "question2": 1}
については
、それはquestion1
が13
ポイントを持っていること、もう一つは1
を持っているなど
私はu
とv
質問とx
とy
点の間の問題のすべてのサブセットを作成しようとしていました。ここで
class GrowingList(list):
def __setitem__(self, index, value):
if index >= len(self):
self.extend([None]*(index + 1 - len(self)))
list.__setitem__(self, index, value)
questions = {"Q1":5, "Q2":1, "Q3": 1, "Q4": 4}
u = 1 #
v = 3 # between u and v questions
x = 1 #
y = 7 #between x and y points
solution = GrowingList()
def main():
Backtracking(0)
def Backtracking(k):
for c in questions.keys():
solution[k] = c
# print ("candidate: ", solution)
if not reject(k):
# print ("not rejected: ", solution)
if accept(k):
print (solution)
if (k == v-1):
return ;
else:
Backtracking(k+1)
def reject(k):
if len(solution) != len(set(solution)): #check whether the question already exists
return True
points = 0
for q in solution:
if q in questions:
points = points + questions[q]
if points >y or k+1 > v: #check if the candidate solution has between the given number of questions and points
return True
return False
def accept(k):
if len(solution) != len(set(solution)): #check whether the question already exists
return False
points = 0
for q in solution:
if q in questions:
points = points + questions[q]
if x <= points <= y and u <= k+1 <= v: #if the candidate solution has between the given number of points and questions, we found a solution
return True
return False
main()
u
= 1のためにそう
、= 3 v
、x
= 1とy
= 7私だけ
['Q1']
['Q1', 'Q2']
['Q1', 'Q2', 'Q3']
['Q2', 'Q4', 'Q3']
['Q2', 'Q1', 'Q3']
['Q2', 'Q1', 'Q3']
['Q2', 'Q4', 'Q3']
取得しています。しかし、私は多くのことを欠場します1つの要素の質問のサブセットの残りの部分など、解の
['Q2']
['Q3']
['Q4']
も解決策に含める必要があります。
どのような答えがOKでしょうか?バックトラッキングだけを受け入れるのですか?問題自体はかなり簡単なので。 – Elmex80s
本当は、しかし、私はバックトラックをやってみた。 –