1. Дан двумерный массив, который содержит буквы английского алфавита в нижнем регистре.
2. Метод detectAllWords должен найти все слова из words в массиве crossword.
3. Элемент(startX, startY) должен соответствовать первой букве слова, элемент(endX, endY) — последней.
text — это само слово, располагается между начальным и конечным элементами
4. Все слова есть в массиве.
5. Слова могут быть расположены горизонтально, вертикально и по диагонали как в нормальном, так и в обратном порядке.
6. Метод main не участвует в тестировании.
Требования:
1. В классе Solution должен существовать метод detectAllWords.
2. В классе Solution должен существовать класс Word.
3. Метод detectAllWords должен быть статическим.
4. Метод detectAllWords должен быть публичным.
5. Метод detectAllWords должен возвращать список всех слов в кроссворде (согласно условию задачи).
package com.javarush.task.task20.task2027;
import java.util.ArrayList;
import java.util.List;
/*
Кроссворд
*/
public class Solution {
public static int[][] crossword;
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'}
};
Solution.crossword = crossword;
detectAllWords(crossword, "home", "same", "rpl", "m" , "lpr", "none");
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
List<Word> listOfWords = new ArrayList<>();
for (String word:words) {
byte[] letters = word.getBytes();
int l = word.length() - 1;
int lastLetter = letters[word.length() - 1];
Word word1 = new Word(word);
for (int i = 0; i < crossword.length; i++) {
for (int j = 0; j < crossword[0].length; j++) {
if (letters.length != 1) {
if (letters[0] == (crossword[i][j])) {
if ((i + l < crossword.length) && (lastLetter == (crossword[i + l][j]) && isThatWord(word, i, j))) {
word1.setStartPoint(j, i);
word1.setEndPoint(j, i + l);
}
if ((i - l >= 0) && (lastLetter == (crossword[i - l][j]) && isThatWord(word, i, j))) {
word1.setStartPoint(j, i);
word1.setEndPoint(j, i - l);
}
if ((j + l < crossword[0].length) && (lastLetter == (crossword[i][j + l]) && isThatWord(word, i, j))) {
word1.setStartPoint(j, i);
word1.setEndPoint(j + l, i);
}
if ((j - l >= 0) && (lastLetter == (crossword[i][j - l]) && isThatWord(word, i, j))) {
word1.setStartPoint(j, i);
word1.setEndPoint(j - l, i);
}
if ((i + l < crossword.length && j + l < crossword[0].length) && (lastLetter == (crossword[i + l][j + l]) && isThatWord(word, i, j))) {
word1.setStartPoint(j, i);
word1.setEndPoint(j + l, i + l);
}
if ((i - l >= 0 && j - l >= 0) && (lastLetter == (crossword[i - l][j - l]) && isThatWord(word, i, j))) {
word1.setStartPoint(j, i);
word1.setEndPoint(j - l, i - l);
}
if ((i - l >= 0 && j + l < crossword[0].length) && (lastLetter == (crossword[i - l][j + l]) && isThatWord(word, i, j))) {
word1.setStartPoint(j, i);
word1.setEndPoint(j + l, i - l);
}
if ((i + l < crossword.length && j - l >= 0) && (lastLetter == (crossword[i + l][j - l]) && isThatWord(word, i, j))) {
word1.setStartPoint(j, i);
word1.setEndPoint(j - l, i + l);
}
}
}
if (word.length()==1){
if (letters[0] == (crossword[i][j])) {
word1.setStartPoint(j, i);
word1.setEndPoint(j, i);
}
}
}
}
listOfWords.add(word1);
}
for (Word word : listOfWords) {
System.out.println(word.toString());
}
return listOfWords;
}
private static boolean isThatWord(String word, int x, int y) {
byte[] letters = word.getBytes();
int i = x;
int j = y;
int count = 1;
for (int k = 1; k < word.length(); k++) {
if (crossword.length > i + k) { //Горизонтальная секция
if (letters[k] == (crossword[i + k][j])){
count++;
}
}
if (i - k >= 0) {
if (letters[k] == (crossword[i - k][j])){
count++;
}
}
if (crossword[0].length > j + k) { //Вертикальная секция
if (letters[k] == (crossword[i][j + k])){
count++;
}
}
if (j - k >= 0) {
if (letters[k] == (crossword[i][j - k])){
count++;
}
}
if (crossword.length > i + k && crossword[0].length > j + k) { //1-я диагональная секция
if (letters[k] == (crossword[i + k][j + k])){
count++;
}
}
if (i - k >= 0 && j - k >= 0) {
if (letters[k] == (crossword[i - k][j - k])){
count++;
}
}
if (crossword.length > i + k && j - k >= 0) { //2-я диагональная секция
if (letters[k] == (crossword[i + k][j - k])){
count++;
}
}
if (i - k >= 0 && crossword[0].length > j + k) {
if (letters[k] == (crossword[i - k][j + k])){
count++;
}
}
if (count == word.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);
}
}
}
Вывод вот такой:
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
rpl - (3, 3) - (1, 3)
m - (0, 3) - (0, 3)
lpr - (1, 3) - (3, 3)
none - (0, 0) - (0, 0)
Валидатору не нравится 5-й пункт.
Что не так?)