在网格中搜索词(Searching for a word in a grid)

2019-10-19 03:07发布

我试图写一个函数,它的字母正方形格子,并给出一个单词从单词列表中找到,它会搜索其水平,垂直或斜(也向后看在每种情况下)。 我试着写各种方式这个功能没有成功所以在想,如果我的一般算法响起确定和实施的。

  • 返回坐标为到处单词的第一个字母时所以像[row,col] = find(grid==words(2))与字是词的列表和网格是方形矩阵。 因此,这将中搜索grid在第二个字words

  • 对于这封信的每次出现垂直或水平移动和对角线在各个方向上的字的长度,如果最后一个字母是我们每个字符从第一个找店到最后作为一个词的词的最后一个字母阵列。

  • 这些词比较,我们正在寻找的话,如果有匹配画一条线。

思考?

Answer 1:

考虑到一个字符数组和子串沿水平,垂直和对角线都找到方向:

A = char(randi(16,7,10)+'a'-1)
A =
  ilhpcdchkl
  ooaecloocd
  kogajcdkpg
  imlnnbiihf
  bigoahciin
  afjfjdhgmp
  pejcdfnmke
% horizontal string in row 4, starting at col 5
cH = [4 5]; l = 4; % strings of length 4
sh = A(cH(1),cH(2)+(0:l-1))
sh =
  nbii

% vertical string in col 6, starting at row 3
cV = [2 6];
sv = A(cV(1)+(0:l-1),cV(2)).' %'
sv =
  lcbh

% diagonal (downward) string starting at row 3, col 7
cD = [3 7];
sd = A((cD(1)+(0:l-1))+size(A,1)*((cD(2)+(0:l-1))-1))
sd =
  diip

% diagonal (upward) string starting at row 5, col 2
cU = [5 2]
su = A((cU(1)-(0:l-1))+size(A,1)*((cU(2)+(0:l-1))-1))
su =
  ilac

开始,可以搜索字符串矩阵的行的功能:

function ij = strsearch(A,s)

C = mat2cell(A,ones(size(A,1),1),size(A,2));
matches = strfind(C,s);
rows = find(~cellfun(@isempty,matches));
if ~isempty(rows),
    cols = vertcat(matches{rows});
else
    cols = [];
end
ij = [rows cols];

例如,这给出了在水平字符串的(行,列)位置sh在基质A

>> ij = strsearch(A,sh)
ij =
     4     5

这是伟大的水平的串,但我们需要的是在所有的方位和方向进行搜索的能力。 我们一个新的功能,称之为wordsearch ,这将输出如下:

>> matches = wordsearch(A,sh)
matches = 
          start: [4 5]
    orientation: 'horizontal'
      direction: 0  % forward
>> matches = wordsearch(A,sv)
matches = 
          start: [2 6]
    orientation: 'vertical'
      direction: 0
>> matches = wordsearch(A,sd)
matches = 
          start: [3 7]
    orientation: 'downward diagonal'
      direction: 0
>> matches = wordsearch(A,su)
matches = 
          start: [5 2]
    orientation: 'upward diagonal'
      direction: 0
>> matches = wordsearch(A,fliplr(sh))
matches = 
          start: [4 8] % sh goes from column 5 to 8, in row 4
    orientation: 'h'
      direction: 1  % backward

为了得到这一点,我们可以建立在strsearch通过转置矩阵搜索水平和垂直的发生。 向后事件可以通过翻转输入字符串被发现。 要搜索对角线,我们可以使用arrayfundiag提取对角线和以类似的方式进行搜索。

一般的搜索功能:

function ij = wordsearcher(A,s,orientation,order)
s = s(:).'; %' ensure row vector
if order, s = fliplr(s); end
switch lower(orientation(1))
    case 'h'
        ij = strsearch(A,s);
        if order && ~isempty(ij), ij(:,2) = ij(:,2) + numel(s) - 1; end
    case 'v'
        ij = fliplr(strsearch(A.',s)); %'
        if order && ~isempty(ij), ij(:,1) = ij(:,1) + numel(s) - 1; end
    case 'd' % down-right diagonals
        Cdiags = arrayfun(@(k)diag(A,k).',-size(A,1)+1:size(A,2)-1,'uni',0); %'
        matches = strfind(Cdiags,s);
        k = find(~cellfun(@isempty,matches));
        if isempty(k), ij=[]; return; end
        row =  (k<=size(A,1)) .* (size(A,1) - k) + [matches{k}];
        col = ~(k<=size(A,1)) .* (k - size(A,1)) + [matches{k}];
        ij = [row; col].'; %'
        if order, ij = ij+numel(s)-1; end
    case 'u' % up-right diagonals
        Cdiags = arrayfun(@(k)diag(flipud(A),k).', ... %' flip A up-down
                                          -size(A,1)+1:size(A,2)-1,'uni',0);
        matches = strfind(Cdiags,s);
        k = find(~cellfun(@isempty,matches));
        if isempty(k), ij=[]; return; end
        row = ~(k<=size(A,1)) .* (size(A,1) - k) + k - [matches{k}] + 1;
        col = ~(k<=size(A,1)) .* (k - size(A,1)) + [matches{k}];
        ij = [row; col].'; %'
        if order, ij=bsxfun(@plus,ij,numel(s)*[-1 1]); end
    otherwise
        error('bad orientation')
end

包装与循环在所有方向/方向进行搜索,以获取wordsearch功能:

function matches = wordsearch(A,s)
matches = struct('start',[],'orientation',[],'direction',[]);
n=1; o='hvdu';
ostr = {'horizontal','vertical','downward diagonal','upward diagonal'};
for id=0:1,
    for io=1:numel(o),
        ij = wordsearcher(A,s,o(io),id);
        if ~isempty(ij),
            matches(n).start = ij;
            matches(n).orientation = ostr{io};
            matches(n).direction = id;
            n = n+1;
        end
    end
end

我希望这个作品。



文章来源: Searching for a word in a grid