필터 지우기
필터 지우기

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 CenterFile Exchange에서 Word games에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by