2

我有 4x4 字母表,我想在那里找到所有可能的路径。他们是词的候选者。我对变量“used”有疑问它是一个列表,其中包括路径已经存在的所有位置,因此它不会再去那里。每条路径都应该有一个使用列表。但它不能正常工作。例如,我有一个打印当前单词和使用列表的测试打印。有时这个词只有一个字母,但路径已经遍历了所有 16 个单元格/索引。

大小为 8 的 for 循环适用于所有可能的方向。主函数执行 16 次追逐函数 - 每个可能的起点执行一次。移动函数在移动到特定方向后返回索引。并且 is_allowed 测试是否允许移动到某个部门。样本输入:oakaoastsniuttot。(4x4 表,其中前 4 个字母是第一行等)示例输出:可以在某个单词的字典中找到的所有真实单词在我的情况下,它可能会输出一个或两个单词,但不是几乎全部,因为它认为某些单元格被使用,即使它们不是。

def chase(current_place, used, word):

   used.append(current_place)   #used === list of indices that have been used
   word += letter_list[current_place]
   if len(word)>=11:
       return 0
   for i in range(3,9):
       if len(word) == i and word in right_list[i-3]:  #right_list === list of all words
        print word
        break
   for i in range(8):
      if is_allowed(current_place, i) and (move(current_place, i) not in used):    
          chase(move(current_place, i), used, word)
4

1 回答 1

2

问题是只有一个used列表可以传递。您有两个选项可以解决此问题chase()

  1. 制作一份副本used并使用该副本。
  2. 从函数返回之前,请撤消append()开始时所做的操作。
于 2013-01-25T19:34:30.593 回答