利用BFS广度优先搜索,搜索字母矩阵中的单词

1.题目:

给定一个字母矩阵,找到矩阵中所有的单词。

1.1只能斜线连接字母

1.2可以斜线和直线连接字母,每个字母只能用一次

1.3可以斜线和直线连接字母,每个字母可以使用多次

2.解答:

这里只做第二题,第二题做出来,第一题和第三题都不在话下,代码如下:

  1. import enchant  
  2. import time  
  3. d = enchant.Dict('en_US')  
  4.     
  5. matrix = [  
  6.     ['H''T''Q''Z''A'],  
  7.     ['F''E''O''H''P'],  
  8.     ['O''M''L''M''B'],  
  9.     ['C''S''O''L''Q'],  
  10.     ['H''X''D''K''O']  
  11. ]  
  12.     
  13. # 1.设置搜索起点  
  14. # 2.每次深度加一进行搜索,然后判断  
  15. # 3.对搜索过的位置,要做标记,不能重复搜索  
  16.     
  17. class SearchWords(object):  
  18.     
  19.     def __init__(self, matrix):  
  20.         self.max_x = len(matrix[0])  
  21.         self.max_y = len(matrix)  
  22.     
  23.         self.matrix = matrix  
  24.         self.result = []  
  25.         self.visited = set()  是否已经搜索过  
  26.     
  27.         self.temp_list = []  存放需要下一步继续搜索的单词  
  28.     
  29.     def search_onestep(self, start_x, start_y, word, direct_x, direct_y):  
  30.         做标记,表明这个点已经搜索过了  
  31.         self.visited.add((start_x,start_y))  
  32.     
  33.         """搜索,指定方向走一步"""  
  34.         new_x = start_x + direct_x  
  35.         new_y = start_y + direct_y  
  36.     
  37.         判断边界条件  
  38.         if new_x >= self.max_x or new_y >= self.max_y or new_x < 0 or new_y < 0:  
  39.             return  
  40.     
  41.         判断是否已经搜索过了  
  42.         if (new_x,new_y) in self.visited:  
  43.             return  
  44.         新的字母  
  45.         word = word + self.matrix[new_x][new_y]  
  46.     
  47.         判断是不是单词  
  48.         if self.is_word(word):  
  49.             print("这是一个单词",word)  
  50.             self.result.append(word)  
  51.     
  52.         判断是不是单词开头  
  53.         if self.is_words_head(word):  
  54.             print("加入继续搜索列表中:",word)  
  55.             self.temp_list.append(word)  
  56.     
  57.             return word,new_x,new_y  
  58.     
  59.     def is_word(self, word):  
  60.         return d.check(word)  
  61.     
  62.     
  63.     def search_eight_directions(self,start_x, start_y, word):  
  64.         """搜索八个方向"""  
  65.         for x in range(-1,2):  
  66.             for y in range(-1,2):  
  67.                 if x == 0 and y == 0:  
  68.                     continue  
  69.     
  70.                 new_info = self.search_onestep(start_x, start_y, word, x, y)  
  71.                 if new_info:  
  72.                     print('新的坐标',new_info)  
  73.                     new_word,new_x,new_y = new_info  
  74.     
  75.                     self.search_eight_directions(new_x, new_y, new_word)  
  76.     
  77.     def is_words_head(self,word):  
  78.         """判断当前字符串是不是一个单词的开头"""  
  79.         for likely_word in d.suggest(word):  
  80.             if likely_word.startswith(word):  
  81.                 return True  
  82.         return False  
  83.     
  84.     
  85.     def run(self):  
  86.         """启动函数"""  
  87.         for x in range(self.max_x):  
  88.             for y in range(self.max_y):  
  89.                 self.search_eight_directions(x, y,self.matrix[x][y])  
  90.                 self.visited = set()  
  91.         去重  
  92.         result = set(self.result)  
  93.         print(result)  
  94.     
  95. if __name__ == '__main__':  
  96.     time1 = time.time()  
  97.     s = SearchWords(matrix)  
  98.     s.run()  
  99.     time2 = time.time()  
  100.     print("总共耗时:{}".format(time2-time1))

3.大致思路

- 设置搜索的起点,每一次搜索,深度增加1

- 对每次广度搜索都要做标记,避免重复使用字母

- 引入enchant模块,对字母串进行判断,包括是不是单词,以及是不是某个单词的开头

- 利用递归,每次深度加一搜索后,获取需要的参数,再次进行调用

 

转载于:https://www.cnblogs.com/daigua/p/9511945.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值