Написал такой пока неоптимизированный код. Ищет все слова косворда в любых направлениях, заносит их в список, потом выводит слово и координаты. Вроде все по заданию, но валидатор выдает ошибку - "Размер списка возвращаемого методом detectAllWords отличается от правильного!". Не понимаю в чем проблема.
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");
System.out.println();
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
List<Word> foundWords = new ArrayList<>();
for(String word : words) {
for (int y = 0; y < crossword.length; y++) {
for (int x = 0; x < crossword[y].length; x++) {
if (crossword[y][x] == word.charAt(0)) {
if (y >= word.length() - 1 && crossword[y - 1][x] == word.charAt(1)) {
Word lookingWord = new Word(word);// поиск по вертикали вверх
boolean symbolNotFound = false;
for (int i = 2; i < word.length(); i++) {
if (crossword[y - i][x] == word.charAt(i)) {
lookingWord.setStartPoint(x, y);
lookingWord.setEndPoint(x, y - i);
} else symbolNotFound = true;
}
if (foundWords.contains(lookingWord)) continue;
if (symbolNotFound) continue;
foundWords.add(lookingWord);
}
if (y + word.length() - 1 <= crossword.length && crossword[y + 1][x] == word.charAt(1)) { // поиск по вертикали вниз
Word lookingWord = new Word(word);
boolean symbolNotFound = false;
for (int i = 2; i < word.length(); i++) {
if (crossword[y + i][x] == word.charAt(i)) {
lookingWord.setStartPoint(x, y);
lookingWord.setEndPoint(x, y + i);
} else symbolNotFound = true;
}
if (foundWords.contains(lookingWord)) continue;
if (symbolNotFound) continue;
foundWords.add(lookingWord);
}
if (x >= word.length() - 1 && crossword[y][x - 1] == word.charAt(1)) { // поиск по горизонтали назад
boolean symbolNotFound = false;
Word lookingWord = new Word(word);
for (int i = 2; i < word.length(); i++) {
if (crossword[y][x - i] == word.charAt(i)) {
lookingWord.setStartPoint(x, y);
lookingWord.setEndPoint(x - i, y);
} else symbolNotFound = true;
}
if (foundWords.contains(lookingWord)) continue;
if (symbolNotFound) continue;
foundWords.add(lookingWord);
}
if (x + word.length() - 1 <= crossword[y].length && crossword[y][x + 1] == word.charAt(1)) { // поиск по горизонтали вперед
boolean symbolNotFound = false;
Word lookingWord = new Word(word);
for (int i = 2; i < word.length(); i++) {
if (crossword[y][x + i] == word.charAt(i)) {
lookingWord.setStartPoint(x, y);
lookingWord.setEndPoint(x + i, y);
} else symbolNotFound = true;
}
if (foundWords.contains(lookingWord)) continue;
if (symbolNotFound) continue;
foundWords.add(lookingWord);
}
if (y >= word.length() - 1 && x >= word.length() - 1 && crossword[y - 1][x - 1] == word.charAt(1)) { // поиск на искосок назад-вверх
boolean symbolNotFound = false;
Word lookingWord = new Word(word);
for (int i = 2; i < word.length(); i++) {
if (crossword[y - i][x - i] == word.charAt(i)) {
lookingWord.setStartPoint(x, y);
lookingWord.setEndPoint(x - i, y - i);
} else symbolNotFound = true;
}
if (foundWords.contains(lookingWord)) continue;
if (symbolNotFound) continue;
foundWords.add(lookingWord);
}
if (y + word.length() - 1 <= crossword.length && x + word.length() - 1 <= crossword[y].length && crossword[y + 1][x + 1] == word.charAt(1)) { // поиск на искосок вперед-вниз
boolean symbolNotFound = false;
Word lookingWord = new Word(word);
for (int i = 2; i < word.length(); i++) {
if (crossword[y + i][x + i] == word.charAt(i)) {
lookingWord.setStartPoint(x, y);
lookingWord.setEndPoint(x + i, y + i);
} else symbolNotFound = true;
}
if (foundWords.contains(lookingWord)) continue;
if (symbolNotFound) continue;
foundWords.add(lookingWord);
}
if (y >= word.length() - 1 && x + word.length() - 1 <= crossword[y].length && crossword[y - 1][x + 1] == word.charAt(1)) { // поиск на искосок вперед-вверх
boolean symbolNotFound = false;
Word lookingWord = new Word(word);
for (int i = 2; i < word.length(); i++) {
if (crossword[y - i][x + i] == word.charAt(i)) {
lookingWord.setStartPoint(x, y);
lookingWord.setEndPoint(x + i, y - i);
} else symbolNotFound = true;
}
if (foundWords.contains(lookingWord)) continue;
if (symbolNotFound) continue;
foundWords.add(lookingWord);
}
if (y + word.length() - 1 <= crossword.length && x >= word.length() - 1 && crossword[y + 1][x - 1] == word.charAt(1)) { // поиск на искосок назад-вниз
boolean symbolNotFound = false;
Word lookingWord = new Word(word);
for (int i = 2; i < word.length(); i++) {
if (crossword[y + i][x - i] == word.charAt(i)) {
lookingWord.setStartPoint(x, y);
lookingWord.setEndPoint(x - i, y + i);
} else symbolNotFound = true;
}
if (foundWords.contains(lookingWord)) continue;
if (symbolNotFound) continue;
foundWords.add(lookingWord);
}
}
}
}
}
System.out.println(foundWords.toString());
return foundWords;
}
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);
}
}
}