Question3

#每一个节点可以走的位置
zhaofa={
0:[4,6],
1:[6,8],
2:[7,9],
3:[4,8],
4:[3,9,0],
5:[],
6:[1,7,0],
7:[2,6],
8:[1,3],
9:[2,4]
}

level=0
times=0
N=4
#当前的位置
ongoing_nodes=[1]
#判断是否结束   1 到达了N步  2 前边没有可以走的
while(level<=N or len(ongoing_nodes)==0):
    print (str(level))
    print(ongoing_nodes)
    #判断当前要走的节点集合中有没有包含目标节点  如果有就去除并且计数器加一
    while True:
        if 9 in ongoing_nodes:
            times+=1
            ongoing_nodes.remove(9)
        else:
            break
    #广度优先搜索   开始下一步走
    next_step_nodes=[]
    for pos in ongoing_nodes:
        next_step_nodes.extend(zhaofa[pos])
    # 走到下一步
    ongoing_nodes=next_step_nodes
    #层数加一
    level+=1
print(times)

输出结果是

0
[1]
1
[6, 8]
2
[1, 7, 0, 1, 3]
3
[6, 8, 2, 6, 4, 6, 6, 8, 4, 8]
4
[1, 7, 0, 1, 3, 7, 9, 1, 7, 0, 3, 9, 0, 1, 7, 0, 1, 7, 0, 1, 3, 3, 9, 0, 1, 3]
3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class Question: def __init__(self, stem, options, answer): self.stem = stem self.options = options self.answer = answerclass QuestionBank: def __init__(self): self.questions = [] def add_question(self, question): self.questions.append(question) def remove_question(self, question): self.questions.remove(question) def get_random_questions(self, num): return random.sample(self.questions, num)class Paper: def __init__(self, questions): self.questions = questions self.answers = {} def answer_question(self, question, answer): self.answers[question] = answer def get_score(self): score = 0 for question, answer in self.answers.items(): if answer == question.answer: score += 1 return scoreclass Grader: def __init__(self, paper): self.paper = paper def grade(self): return self.paper.get_score()# Example usagequestion1 = Question("What is the capital of France?", ["Paris", "London", "Berlin", "Madrid"], "Paris")question2 = Question("What is the largest planet in the solar system?", ["Mercury", "Venus", "Earth", "Jupiter"], "Jupiter")question3 = Question("What is the highest mountain in the world?", ["K2", "Mount Everest", "Makalu", "Cho Oyu"], "Mount Everest")question_bank = QuestionBank()question_bank.add_question(question1)question_bank.add_question(question2)question_bank.add_question(question3)paper = Paper(question_bank.get_random_questions(2))paper.answer_question(question1, "Paris")paper.answer_question(question2, "Jupiter")grader = Grader(paper)score = grader.grade()print("Your score is:", score)将这个代码转为C++的
05-30

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值