не понимаю.
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'},
{'o', 'h', 'g', 'r', 'o', 'v'},
{'m', 'l', 'p', 'r', 'r', 'h'},
{'e', 'o', 'e', 'e', 'j', 'j'}
};
System.out.println(detectAllWords(crossword, "home", "same"));
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
List<Word> toReturn = new ArrayList<>();
for (String word : words) {
int first = word.charAt(0);
int last = word.charAt(word.length() - 1);
for (int y1 = 0; y1 < crossword.length; y1++) {
for (int x1 = 0; x1 < crossword[y1].length; x1++)
if (crossword[y1][x1] == first) {
for (int y2 = 0; y2 < crossword.length; y2++) {
for (int x2 = 0; x2 < crossword[y2].length; x2++)
if (crossword[y2][x2] == last) {
if (wordCheck(crossword, word, x1, y1, x2, y2)) {
Word result = new Word(word);
result.setStartPoint(x1, y1);
result.setEndPoint(x2, y2);
toReturn.add(result);
}
}
}
}
}
}
return toReturn;
}
private static boolean wordCheck(int[][] crossword, String word, int x1, int y1, int x2, int y2) {
if (Math.abs(x1 - x2) - (word.length() - 1) == 0 || Math.abs(y1 - y2) - (word.length() - 1) == 0) {
char[] parts = word.toCharArray();
int count = 0, tempX = x1, tempY = y1;
for (int i = 0; i < parts.length; i++) {
if (parts[i] == crossword[tempY][tempX]) {
count++;
tempX += x1 > x2 ? -1 : x1 < x2 ? 1 : 0;
tempY += y1 > y2 ? -1 : y1 < y2 ? 1 : 0;
}
}
if (count == parts.length) return true;
}
return false;
}
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);
}
}
}