Azzera filtri
Azzera filtri

How would you check diagonally on a wordsearch?

3 visualizzazioni (ultimi 30 giorni)
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)

Risposta accettata

Walter Roberson
Walter Roberson il 27 Nov 2015

Più risposte (1)

John D'Errico
John D'Errico il 27 Nov 2015
Modificato: John D'Errico il 27 Nov 2015
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 Commenti
Shawn Simon
Shawn Simon il 27 Nov 2015
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 il 28 Nov 2015
Modificato: John D'Errico il 28 Nov 2015
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.

Accedi per commentare.

Categorie

Scopri di più su Word games in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by