ответы правильные, также тестировал на матрице:
{'f', 'e', 'e', 'e', 'l', 'e'},
{'u', 's', 'n', 'n', 'n', 'o'},
{'l', 'e', 'n', 'o', 'n', 'e'},
{'m', 'm', 'n', 'n', 'n', 'h'},
{'p', 'e', 'e', 'e', 'j', 'e'},
с поиском слова "one", находит 8, но валидатор против...
60ую строку с меткой notReaped: закоментировал, так же пробовал для поиска лишь одного варианта...
package com.javarush.task.task20.task2027;
import java.util.ArrayList;
import java.util.List;
/*
Кроссворд
*/
public class Solution {
public static void main(String[] args) {
int[][] crossword = new int[][]{
{'f', 'e', 'e', 'e', 'l', 'e'},
{'u', 's', 'n', 'n', 'n', 'o'},
{'l', 'e', 'n', 'o', 'n', 'e'},
{'m', 'm', 'n', 'n', 'n', 'h'},
{'p', 'e', 'e', 'e', 'j', 'e'},
};
List<Word> forPrint = detectAllWords(crossword, "one");
System.out.println(forPrint.size());
for (Word word: forPrint){
System.out.println(word);
}
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
ArrayList<Word> answers = new ArrayList<>();
for (String word: words){
char[] charsOfWord = word.toCharArray();
int wordLenth = charsOfWord.length;
//движения по горизонтали (x-columns) возможны 1, 0, -1
//движения по вертикали (y-lists) возможны 1, 0, -1
ArrayList<Integer> horizontMove = new ArrayList<>();
ArrayList<Integer> verticalMove = new ArrayList<>();
//ищем первую букву слова в массиве
for (int y = 0; y < 5; y++){
for (int x = 0; x < 6; x++){
if(charsOfWord[0]==(char)crossword[y][x]){
horizontMove.clear();
verticalMove.clear();
horizontMove.add(0);
verticalMove.add(0);
if (6 -(x) >= wordLenth) horizontMove.add(1);
if ((x+1) >= wordLenth) horizontMove.add(-1);
if (5 - (y) >= wordLenth) verticalMove.add(1);
if ((y+1) >= wordLenth)verticalMove.add(-1);
//проверяем следующую букву по всевозможным направлениям
notReaped:
for (int xMove : horizontMove) {
for (int yMove : verticalMove) {
boolean scanToEnd = true;
int actX = x;
int actY = y;
if (!(yMove == 0 && xMove == 0)){
for (char isTheSame : charsOfWord){
if (isTheSame !=(char)crossword[actY][actX]){
scanToEnd = false;
break;
}
actX += xMove;
actY += yMove;
}
if (scanToEnd){
Word founded = new Word(word);
founded.setStartPoint(x,y);
founded.setEndPoint(actX-xMove,actY-yMove);
answers.add(founded);
//break notReaped;
}
}
}
}
}
}
}
}
return answers;
}
public static class Word {
private String text;
private int startX;
private int startY;
private int endX;
private int endY;
public Word(String text) {
this.text = text;
}
public void setStartPoint(int i, int j) {
startX = i;
startY = j;
}
public void setEndPoint(int i, int j) {
endX = i;
endY = j;
}
@Override
public String toString() {
return String.format("%s - (%d, %d) - (%d, %d)", text, startX, startY, endX, endY);
}
}
}