Вывод верный
добавил даже слово сове, выдает верно
Вопрос: валидатору что надо чтоб проверялись еще буквы в середине слова?
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'}
};
List <Word> list = detectAllWords(crossword, "home", "same");
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
for (Word w: list) {
System.out.println(w.toString());
}
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
List <Word> list = new ArrayList<>();
for (String s: words)
{
list.add(new Word(s));
}
for (Word w: list)
{
char [] chars = w.text.toCharArray();
for (int j = 0; j <crossword.length ; j++) {
for (int k = 0; k <crossword[j].length ; k++) {
if (chars[0] == crossword[j][k]&&w.startX==0&&w.startY==0) {
w.setStartPoint(k, j); //5,3
int a = ((w.startX + (chars.length - 1)) < crossword.length) ? (w.startX + (chars.length - 1)) : w.startX;//5
int b = ((w.startY + (chars.length - 1)) < crossword[j].length) ? (w.startY + (chars.length - 1)) : w.startY;//3
int c = (0 <= (w.startX - (chars.length - 1))) ? (w.startX - (chars.length - 1)) : w.startX;//5
int d = (0 <= (w.startY - (chars.length - 1))) ? (w.startY - (chars.length - 1)) : w.startY;//0
if (chars[chars.length - 1] == crossword[b][a]) w.setEndPoint(a, b);
if (chars[chars.length - 1] == crossword[d][a]) w.setEndPoint(a, d);
if (chars[chars.length - 1] == crossword[b][c]) w.setEndPoint(c, b);
if (chars[chars.length - 1] == crossword[d][c]) w.setEndPoint(c, d);
}
// if ((chars[chars.length - 1] == crossword[w.startX + chars.length][w.startY + chars.length])
// && (w.startX + chars.length < crossword.length) && (w.startY + chars.length < crossword.length))
// w.setEndPoint(k, j);
// if ((chars[chars.length - 1] == crossword[w.startX + chars.length][w.startY - chars.length])
// && (w.startX + chars.length < crossword.length) && (w.startY - chars.length < crossword.length))
// w.setEndPoint(k, j);
// if ((chars[chars.length - 1] == crossword[w.startX - chars.length][w.startY + chars.length])
// && (w.startX - chars.length < crossword.length) && (w.startY + chars.length < crossword.length))
// w.setEndPoint(k, j);
// if ((chars[chars.length - 1] == crossword[w.startX - chars.length][w.startY - chars.length])
// && (w.startX - chars.length < crossword.length) && (w.startY - chars.length < crossword.length))
// w.setEndPoint(k, j);
}
}
}
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);
}
}
}