フィルターのクリア

How would you check diagonally on a wordsearch?

3 ビュー (過去 30 日間)
Shawn Simon
Shawn Simon 2015 年 11 月 27 日
編集済み: John D'Errico 2015 年 11 月 28 日
Hello, I am trying to create a function that will find given words in a pre-loaded wordsearch. I have already done checks for words that are left-to-right, right-to-left, top-to-bottom, and bottom-to-top, however I need to do a check for words that are diagonal. I have been trying to come up with my own algorithm for this problem, but it has proven to be quite the adversary. Also, when a word is found, I am to return the row and column of the first letter of the word. I tried using ind2sub but I was getting a value that didn't make any sense. Thank you for any help!
Some information about the wordsearch: It is a 15x15 2 dimensional array filled with characters (letters)

採用された回答

Walter Roberson
Walter Roberson 2015 年 11 月 27 日

その他の回答 (1 件)

John D'Errico
John D'Errico 2015 年 11 月 27 日
編集済み: John D'Errico 2015 年 11 月 27 日
Consider an nxm array, and suppose you wish to generate all diagonal substrings, of length p that go diagonally down to the right. (The other three directions are trivial changes. I'll leave them to the student.)
For example...
n = 5;m = 6;
A = char('a' - 1 + randi(26,[n,m]))
A =
vjnaxx
dziryi
psvjvs
zashsw
plrrjw
p = 3;
[i,j] = ndgrid(1:n-p+1,1:m-p+1);
ind = sub2ind([n,m],i(:),j(:));
A(bsxfun(@plus,ind,[0:(n+1):((p-1)*n+p-1)]))
ans =
vzv
dss
par
jij
zvh
ssr
nrv
ijs
vhj
ays
rvw
jsw
Decide if any of the above rows make a word of length 3. It looks like one of those rows is a word after all. Then repeat for 4 letter words, etc. WTP?
  2 件のコメント
Shawn Simon
Shawn Simon 2015 年 11 月 27 日
Is there a way to get what row and column the word is in? Like something similar to getRows in java?
John D'Errico
John D'Errico 2015 年 11 月 28 日
編集済み: John D'Errico 2015 年 11 月 28 日
Trivially. Once you figure out that 'par' is a word for example, you will see that it is the 3rd in the list. A test like ismember would suffice for that, assuming you have a list of candidate words to search for.
i(3)
ans =
3
j(3)
ans =
1
'par' starts at the (3,1) location in your array, and goes down diagonally to the right.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWord games についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by