Все тесты проходит, но валидацию нет. Прошу вашей помощи
package com.javarush.task.task20.task2027;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
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'}
};
// int[][] crossword = new int[][] {
// {'a', 'a', 'a'},
// {'a', 'o', 'a'},
// {'a', 'a', 'a'}
// };
// int[][] crossword = new int[][] {
// {'a', 'a'},
// {'a', 'a'}
//
// };
//"rrpl","jrra","fsg","kerp","plg","home","same"
List<Word> list = detectAllWords(crossword, "rrpl","jrra","fsg","kerp","plg","home","same","a");
for (Word word : list) {
System.out.println(word);
}
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
List<Word> list = new ArrayList<>();
for (String word : words) {
char[] chars = word.toCharArray();
int j = 0;
int k = 0;
int startX = 0;
int startY = 0;
for (; j < crossword.length; j++) {
for (; k < crossword[j].length; k++) {
int[] startCoords = searchStart(crossword,word,k,j);
if (startCoords != null){
startX = startCoords[0];
startY = startCoords[1];
k = startX;
j = startY;
}
else break;
int endX = 0;
int endY = 0;
if (chars.length == 1){
endX = startX;
endY = startY;
Word w = new Word(word);
w.setStartPoint(startX,startY);
w.setEndPoint(endX,endY);
list.add(w);
}
int[] dx = new int[]{-1,0,1};
int[] dy = new int[]{-1,0,1};
point:
for (int dy1 : dy) {
for (int dx1 : dx) {
int[] endCoords = searchEnd(crossword,word,startX,startY,dx1,dy1);
if (endCoords != null){
endX = endCoords[0];
endY = endCoords[1];
Word w = new Word(word);
w.setStartPoint(startX,startY);
w.setEndPoint(endX,endY);
list.add(w);
}
}
}
}
k=0;
}
}
return list;
}
public static int[] searchEnd(int[][] crossword, String word, int startX, int startY,int dx1,int dy1){
char[] chars = word.toCharArray();
int[] resultCoord = new int[2];
int endX = startX;
int endY = startY;
dif:
for (int i = 1; i < chars.length; i++) {
if (((endY + dy1 >= 0 && endY + dy1 < crossword.length && (dy1 != 0 || dx1 != 0))
&& (endX + dx1 >= 0 && endX + dx1 < crossword.length))
&& chars[i] == crossword[endY + dy1][endX + dx1]){
endY += dy1;
endX += dx1;
if (i == chars.length-1 && (endX >= 0 && endY >=0)){
resultCoord[0] = endX;
resultCoord[1] = endY;
return resultCoord;
}
if (endX + dx1 == crossword[crossword.length-1].length || endY + dy1 == crossword.length){
break;
}
}
else {
resultCoord[0] = startX;
resultCoord[1] = startY;
break dif;
}
}
return null;
}
public static int[] searchStart(int[][] crossword, String word, int jX, int jY){
char[] letter = word.toCharArray();
int[] resultCoord = new int[2];
int k = jX;
int j = jY;
for (; j < crossword.length; j++) {
for (; k < crossword[j].length; k++) {
if (letter[0] == crossword[j][k]){
resultCoord[0] = k;
resultCoord[1] = j;
return resultCoord;
}
}
k = 0;
}
return null;
}
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);
}
}
}