给定x by x字符网格,找到单词及其位置

时间:2018-10-27 10:54:17

标签: python

寻找有关算法/脚本的建议,以寻找以下列表中的单词, 可以读一个词:

  1. 从左到右水平
  2. 从上到下垂直
  3. 从左上角到右下角

返回

L = [['N', 'D', 'A', 'O', 'E', 'L', 'D', 'L', 'O', 'G', 'B', 'M', 'N', 'E'],
 ['I', 'T', 'D', 'C', 'M', 'E', 'A', 'I', 'N', 'R', 'U', 'T', 'S', 'L'],
 ['C', 'L', 'U', 'U', 'E', 'I', 'C', 'G', 'G', 'G', 'O', 'L', 'I', 'I'],
 ['K', 'M', 'U', 'I', 'M', 'U', 'I', 'D', 'I', 'R', 'I', 'A', 'L', 'T'],
 ['E', 'U', 'R', 'T', 'U', 'N', 'G', 'S', 'T', 'E', 'N', 'B', 'V', 'H'],
 ['L', 'I', 'L', 'S', 'L', 'T', 'T', 'U', 'L', 'R', 'U', 'O', 'E', 'I'],
 ['C', 'M', 'A', 'T', 'E', 'T', 'I', 'U', 'R', 'D', 'R', 'C', 'R', 'U'],
 ['I', 'D', 'S', 'C', 'A', 'M', 'A', 'G', 'N', 'E', 'S', 'I', 'U', 'M'],
 ['M', 'A', 'M', 'P', 'D', 'M', 'U', 'I', 'N', 'A', 'T', 'I', 'T', 'I'],
 ['P', 'C', 'N', 'P', 'L', 'A', 'T', 'I', 'N', 'U', 'M', 'D', 'L', 'L'],
 ['H', 'Z', 'E', 'M', 'A', 'N', 'G', 'A', 'N', 'E', 'S', 'E', 'I', 'G'],
 ['M', 'G', 'I', 'T', 'I', 'N', 'R', 'U', 'N', 'O', 'R', 'I', 'T', 'C'],
 ['R', 'I', 'A', 'N', 'N', 'A', 'M', 'E', 'R', 'C', 'U', 'R', 'Y', 'N'],
 ['U', 'O', 'T', 'C', 'C', 'R', 'E', 'P', 'P', 'O', 'C', 'E', 'E', 'R']]

我正在考虑格式

def find_word(filename, word):
    location = find_word_horizontally(grid, word)
    found = False
    if location:
        found = True
        print(word, 'was found horizontally (left to right) at position', location)
    location = find_word_vertically(grid, word)
    if location:
        found = True
        print(word, 'was found vertically (top to bottom) at position', location)
    location = find_word_diagonally(grid, word)
    if location:
        found = True
        print(word, 'was found diagonally (top left to bottom right) at position', location)
    if not found:
        print(word, 'was not found')

def find_word_horizontally(grid, word):

def find_word_vertically(grid, word):

def find_word_diagonally(grid, word):

预期输出:

find_word('word_search_1.txt', 'PLATINUM')
PLATINUM was found horizontally (left to right) at position (10, 4)

find_word('word_search_1.txt', 'LITHIUM')
LITHIUM was found vertically (top to bottom) at position (2, 14)

find_word('word_search_1.txt', 'MISS')
LITHIUM was found vertically (top to bottom) at position (2, 5)

2 个答案:

答案 0 :(得分:0)

您可以执行DFS查找这样的单词:

class Find:
  def __init__(self):
    self.dx = [1, 1, 0] #go down, go diag, go right
    self.dy = [0, 1, 1]

  def FindWord(self, grid, word):
    if len(word) == 0:
       return False

    m = len(grid)
    if m == 0:
       return False

    n = len(grid[0])
    if n == 0:
       return False

    for i in range(m):
      for j in range(n):
         for d in range(3): #try all 3 directions
            if self.Helper(grid, word, i, j, 0, m, n, d):
                print("Found word at " + str(i) + "," + str(j))
                return True
    return False

  def Helper(self, grid, word, x, y, k, rows, cols, direction):
    if k == len(word):
       return True

    if grid[x][y] != word[k]:
       return False

    new_x = x + self.dx[direction]
    new_y = y + self.dy[direction]

    return (self.InBound(new_x, new_y, rows, cols) and self.Helper(grid, word, new_x, new_y, k + 1, rows, cols, direction))

  def InBound(self, i, j, rows,cols):
    return (i >= 0 and i < rows and j >= 0 and j < cols)



L = [['N', 'D', 'A', 'O', 'E', 'L', 'D', 'L', 'O', 'G', 'B', 'M', 'N', 'E'],
 ['I', 'T', 'D', 'C', 'M', 'E', 'A', 'I', 'N', 'R', 'U', 'T', 'S', 'L'],
 ['C', 'L', 'U', 'U', 'E', 'I', 'C', 'G', 'G', 'G', 'O', 'L', 'I', 'I'],
 ['K', 'M', 'U', 'I', 'M', 'U', 'I', 'D', 'I', 'R', 'I', 'A', 'L', 'T'],
 ['E', 'U', 'R', 'T', 'U', 'N', 'G', 'S', 'T', 'E', 'N', 'B', 'V', 'H'],
 ['L', 'I', 'L', 'S', 'L', 'T', 'T', 'U', 'L', 'R', 'U', 'O', 'E', 'I'],
 ['C', 'M', 'A', 'T', 'E', 'T', 'I', 'U', 'R', 'D', 'R', 'C', 'R', 'U'],
 ['I', 'D', 'S', 'C', 'A', 'M', 'A', 'G', 'N', 'E', 'S', 'I', 'U', 'M'],
 ['M', 'A', 'M', 'P', 'D', 'M', 'U', 'I', 'N', 'A', 'T', 'I', 'T', 'I'],
 ['P', 'C', 'N', 'P', 'L', 'A', 'T', 'I', 'N', 'U', 'M', 'D', 'L', 'L'],
 ['H', 'Z', 'E', 'M', 'A', 'N', 'G', 'A', 'N', 'E', 'S', 'E', 'I', 'G'],
 ['M', 'G', 'I', 'T', 'I', 'N', 'R', 'U', 'N', 'O', 'R', 'I', 'T', 'C'],
 ['R', 'I', 'A', 'N', 'N', 'A', 'M', 'E', 'R', 'C', 'U', 'R', 'Y', 'N'],
 ['U', 'O', 'T', 'C', 'C', 'R', 'E', 'P', 'P', 'O', 'C', 'E', 'E', 'R']]


inst = Find()
inst.FindWord(L, "TUI")

在这种情况下,输出将为“在1,1处找到单词”

答案 1 :(得分:0)

这是一个生成器,可以使用Numpy操作字母矩阵来完成您要查找的内容。

import numpy as np

def find_word(w, m):
    for r, row in enumerate(m,1):
        s = ''.join(row)
        if w in s:
            yield ('H', r, s.find(w)+1)
    for c, col in enumerate(m.T, 1):
        s = ''.join(col)
        if w in s:
            yield ('V', s.find(w)+1, c)
    for v in range(-max(m.shape), max(m.shape)):
        s = ''.join(m.diagonal(v))
        if w in s:
            p = s.find(w)
            r, c = (abs(v), 0)
            if v > 0:
                r, c = c, r
            r, c = r+p, c+p
            yield ('D', r+1, c+1)

您可以使用输出来填充打印字符串。

m = np.array(L)
d_dict = {'H': 'horizontally (left to right)', 
          'V': 'horizontally (left to right)', 
          'D': 'diagonally (top left to bottom right)'}

for word in ('PLATINUM', 'LITHIUM', 'MIIS'):
    for ret in find_word(word, m):
        d, x, y = ret
        print(f'{word} was found {d_dict[d]} as postion {x,y}')

打印输出:

PLATINUM was found horizontally (left to right) as postion (10, 4)
LITHIUM was found horizontally (left to right) as postion (2, 14)
MIIS was found diagonally (top left to bottom right) as postion (2, 5)