在matlab中解决wordsearch拼图

时间:2013-12-19 06:02:00

标签: matlab octave

发布我的全部代码,希望有人可以帮我调试这个垃圾。真的希望很快得出结论,因为我已经玩弄这个太久了。

我这里有一个函数提取,它传递一个整数网格,它代表我的wordsearch函数中的字母(即A = 1,Z = 26)。给定由int的行向量表示的方向和目标单词,它应该遍历网格以找到第一个字母存在的位置,并且从这里向所有方向移动该单词的长度并提取单词例如如果我们正在寻找[14,1,5,9,2]并且14首先定位在(4,4),我们应该最终得到(4,8)。

然后在搜索功能中比较该单词,如果它与目标单词匹配,则在实际单词搜索的图像上从第一个字母到最后一个字母绘制一条线。

我知道我的if和for循环在某些地方是关闭的,但我发现很难纠正我的代码以便它可以工作。救命!特别是我遇到困难的一件事是控制流量,这样如果在检查包含第一个字母的方格的所有方向后,将评估该字母的下一个实例。这样做最好在哪里?

代码有很多错误,可以用几个指针告诉我需要改变或清理的地方。

%//A function to find a word in a grid.
function test = extract(grid, direction, target) 

%//switch through different cases that allow us to move to any adjacent cell to the current    
switch upper(direction)
    case 1 
        rowdir = -1; 
        coldir = 0; 
    case 2 
        rowdir = -1; 
        coldir = 1; 
    case 3 
        rowdir = 0; 
        coldir = 1; 
    case 4 
        rowdir = 1; 
        coldir = 1; 
    case 5 
        rowdir = 1; 
        coldir = 0; 
    case 6 
        rowdir = 1; 
        coldir = -1; 
    case 7 
        rowdir = 0; 
        coldir = -1; 
    case 8 
        rowdir = -1; 
        coldir = -1;  
end

[i, j] = size(grid); 
len = length(target); 
[row,column] = find(target(1)==grid); %//find the letter of the word we are looking for in grid
%//row and column of last letter having moved in a particular direction
rowN = row + (len-1) * rowdir; 
colN = column + (len-1) * coldir; 

%//trying to say here to only move in a particular direction if we don't go out of bounds. 
%//not sure I've succeeded
if (rowN > 1) | (rowN < i) | (colN > 1) | (colN < j) 
     testword = []; %empty array created
    for index = 1:len 
        index_1 = index-1;
        %//on loop get the letter in adjacent cell for direction we have moved
        word = grid(row + (index_1 * rowdir), column + (index_1 * coldir)); 
        testword{index} = word; %//letters are appended to create word for which we compare.
        %//get co-ords of start letter. change to pixel  co-ordinates so we can evaluate on image
        wordstart = [(row*30)-15, (column*30)-15 ]; 
        wordend = [((row + (len-1 * rowdir))*30)-15, ((column + (len-1 * coldir))*30)-15];
    end 
else
    word = '';
end 

  x1 = wordstart(1);
  x2 = wordend(1);
  y1 = wordstart(2);
  y2 = wordend(2);

  test = [ word , [x1,x2] , [y1,y2]]; %//only way I could think of to get all of these as outputs

end

%//test is the image we want to evaluate on
%//words is the list of words

function trial1 = wordsearch(test, words)

imagesc(test);
colormap(gray);
hold on;

grid = %//grid is a 15x15 matrix

[row, column] = size(grid); 

for iword = 1 : length(words) 
    target = char(words(iword)) - 'a' + 1;
    for i = 1:row 
        for j = 1:column 
            for direction_num = 1:8 %//for each direction
                direction = directions(direction_num, :);
                testword = extract(grid,direction,target); 
                if testword(1)==target %//if word we have extracted equals the target word

%//draw_line function takes x co-ordinates and y co-ordinates and plots line.
                    draw_line(testword(2),testword(3),testword(4),testword(5)); 
                end
            end
        end
    end
end

hold off;
end

@丹

我的提取功能现在看起来像:

[i, j] = size(grid);
len = length(target); 
[row,column] = find(target(1)==grid);
for ii = 1:length(row)
    start_row = row(ii);
    start_column = column(ii); 
    rowN = start_row + len-1 * rowdir; 
    colN = start_column + len-1 * coldir; 
    if (rowN > 1) || (rowN < i) || (colN > 1) || (colN < j) 
        testword = [];
        for index = 1:len
        index_1 = index-1;
        word = grid(start_row + (index_1 * rowdir), start_column + (index_1 * coldir));
        testword{index} = word;
        wordstart = [(start_row*30)-15, (start_column*30)-15 ];
        wordend = [((start_row + (len-1 * rowdir))*30)-15, ((start_column + (len-1 * coldir))*30)-15];
        end 
    else

    end
end 

如果前一个特定的方向超出界限,我会将其作为一个else语句来检查单词?

1 个答案:

答案 0 :(得分:1)

for iword = 1 : length(words) 
    target = char(words(iword)) - 'a' + 1;
    for i = 1:row 
        for j = 1:column 
            for direction_num = 1:8 %//for each direction

对于每个单词,您循环遍历网格中的每个元素(即ij循环),但实际上您并未使用这些i或{{1值。所以这两个循环什么都不做!这是因为您似乎在j函数中执行了所有这些操作。因此放弃这两个循环,它们浪费了过多的时间。

extract功能中,您有一行extract。这将找到 ALL 可能的起点。所以你实际上需要遍历其中的任何一个。因此,我会建议更像是[row,column] = find...而不是if (rowN > 1) | (rowN < i) | (colN > 1) | (colN < j)

for ii = 1:length(row)
    start_row = row(ii);
    start_column = column(ii); 
    %// And now re-use your code, but swap out all your row for start_row and your column for start_column
    .
    .
    .
end

这是将遍历每个可能的起始字母的循环。