Проверял слова со всеми возможными расположениями: сверху вниз, снизу вверх, по диагоналям, обрывающееся слова и т.д, , пробовал комбинации из примеров ниже - все местоположения начала - конца и количество слов в списке - выглядит все ок.
Казалось бы если все слова находятся правильно и количество слов правильное значит и код программы должен быть правльный. Но тем не менее по последнему пункту код проходить валидацию не хочет.
Я уже без понятия, что не так, жду ваших рекомендаций
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', 'd', 'e', 'r', 'l', 'k'},
{'u', 's', 'a', 'm', 'e', 'o'},
{'l', 'n', 'g', 'r', 'o', 'v'},
{'m', 'l', 'p', 'r', 'r', 'h'},
{'p', 'o', 'e', 'e', 'j', 'j'}
};
detectAllWords(crossword, "home", "same");//, "one""pmlpml" "squeeze", "stylish", "unstylish", "lleejj", "jjeell", "qsj", "edf"
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
List<Word> list = new ArrayList<>();
int cnt = 0;
for(int count = 0; count < words.length; count++){
Word word = new Word(words[count]);
for(int i = 0; i < crossword.length; i++){
for(int j = 0; j < crossword[i].length; j++){
if (words[count].charAt(cnt) == crossword[i][j]) {
int k, n;
k = i;
while((++k < crossword.length) && (cnt < words[count].length()) && (words[count].charAt(cnt) == crossword[k][j])) {
cnt++;
}
if (cnt == words[count].length()) {
word.setEndPoint(j, --k);
word.setStartPoint(j, i);
list.add(word);
}
cnt = 1;
n = j;
while((++n < crossword[i].length) && (cnt < words[count].length()) && (words[count].charAt(cnt) == crossword[i][n])) {
cnt++;
}
if(cnt == words[count].length()) {
word.setEndPoint(--n, i);
word.setStartPoint(j, i);
list.add(word);
}
cnt = 1;
k = i;
while((--k >= 0) && (cnt < words[count].length()) && (words[count].charAt(cnt) == crossword[k][j])) {
cnt++;
}
if(cnt == words[count].length()) {
word.setEndPoint(j, ++k);
word.setStartPoint(j, i);
list.add(word);
}
cnt = 1;
n = j;
while ((--n >= 0) && (cnt < words[count].length()) && (words[count].charAt(cnt) == crossword[i][n])) {
cnt++;
}
if(cnt == words[count].length()) {
word.setEndPoint(++n, i);
word.setStartPoint(j, i);
list.add(word);
}
cnt = 1;
k = i;
n = j;
while((++k < crossword.length) & (++n < crossword[i].length) && (cnt < words[count].length()) && (words[count].charAt(cnt) == crossword[k][n])) {
cnt++;
}
if(cnt == words[count].length()) {
word.setEndPoint(--n, --k);
word.setStartPoint(j, i);
list.add(word);
}
cnt = 1;
k = i;
n = j;
while((--k >= 0) & (--n >= 0) && (cnt < words[count].length()) && (words[count].charAt(cnt) == crossword[k][n])) {
cnt++;
}
if(cnt == words[count].length()) {
word.setEndPoint(++n, ++k);
word.setStartPoint(j, i);
list.add(word);
}
cnt = 0;
}
}
}
}
return list;
}
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);
}
}
}