Доброго времени суток!
Вроде все тесты проходит, координаты верно определяет, не понимаю в чем проблема.
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[][]{
{'a', 'a', 'a', 'r', 'l', 'k'},
{'a', 's', 'a', 'm', 'e', 'o'},
{'a', 'a', 'a', 'r', 'o', 'v'},
{'m', 'l', 'p', 'r', 'r', 'h'},
{'p', 'o', 'e', 'e', 'j', 'j'}
};
detectAllWords(crossword, "sa", "same").forEach(System.out::println);
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static class Data {
public static int[][] copyCrossword = null;
public static List<Word> result = new ArrayList<>();
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
String[] copyWords = words;
Data.copyCrossword = crossword;
for (String s : copyWords) {
searchStart(s);
}
return Data.result;
}
public static void searchStart(String s) {
char[] wordAsChars = s.toCharArray();
char initialChar = s.charAt(0);
Word currentWord = null;
for (int y = 0; y < Data.copyCrossword.length; y++) {
for (int x = 0; x < Data.copyCrossword[y].length; x++) {
if (Data.copyCrossword[y][x] == initialChar) {
currentWord = new Word(s);
currentWord.setStartPoint(x,y);
currentWord.setEndPoint(x,y);
currentWord.text = s;
chooseDirection(currentWord, wordAsChars);
}
}
}
}
public static void chooseDirection(Word currentWord, char[] wordAsChars) {
if (wordAsChars.length==1) {
Data.result.add(currentWord);
return;
}
int secondChar = wordAsChars[1];
int x = currentWord.startX;
int y = currentWord.startY;
if ((checkDirection(currentWord, Direction.EAST)) && secondChar == Data.copyCrossword[y][x + 1]) {
checkWord(currentWord, wordAsChars, Direction.EAST);
}
if ((checkDirection(currentWord, Direction.WEST)) && secondChar == Data.copyCrossword[y][x - 1]) {
checkWord(currentWord, wordAsChars, Direction.WEST);
}
if (checkDirection(currentWord, Direction.SOUTH) && (secondChar == Data.copyCrossword[y + 1][x])) {
checkWord(currentWord, wordAsChars, Direction.SOUTH);
}
if (checkDirection(currentWord, Direction.NORTH) && (secondChar == Data.copyCrossword[y - 1][x])) {
checkWord(currentWord, wordAsChars, Direction.NORTH);
}
if (checkDirection(currentWord, Direction.NORTH) && (checkDirection(currentWord, Direction.EAST)) && secondChar == Data.copyCrossword[y - 1][x + 1]) {
checkWord(currentWord, wordAsChars, Direction.NORTH_EAST);
}
if (checkDirection(currentWord, Direction.SOUTH) && (checkDirection(currentWord, Direction.EAST)) && secondChar == Data.copyCrossword[y + 1][x + 1]) {
checkWord(currentWord, wordAsChars, Direction.SOUTH_EAST);
}
if (checkDirection(currentWord, Direction.NORTH) && (checkDirection(currentWord, Direction.WEST)) && secondChar == Data.copyCrossword[y - 1][x - 1]) {
checkWord(currentWord, wordAsChars, Direction.NORTH_WEST);
}
if (checkDirection(currentWord, Direction.SOUTH) && (checkDirection(currentWord, Direction.WEST)) && secondChar == Data.copyCrossword[y + 1][x - 1]) {
checkWord(currentWord, wordAsChars, Direction.SOUTH_WEST);
}
}
public static boolean checkDirection(Word currentWord, Direction direction) {
int wordLength = currentWord.text.length();
int x = currentWord.startX;
int y = currentWord.startY;
switch (direction) {
case EAST:
return wordLength <= Data.copyCrossword[y].length - x;
case WEST:
return wordLength - 1 <= x;
case SOUTH:
return y + wordLength <= Data.copyCrossword.length;
case NORTH:
return wordLength - 1 <= y;
default:
return false;
}
}
public static void checkWord(Word currentWord, char[] wordAsChars, Direction direction) {
Word tempWord = null;
int x = currentWord.startX;
int y = currentWord.startY;
int i = 0;
int j = 0;
int counter = wordAsChars.length;
switch (direction) {
case EAST:
j = 1;
break;
case WEST: {
j = -1;
break;
}
case SOUTH: {
i = 1;
break;
}
case NORTH: {
i = -1;
break;
}
case NORTH_EAST: {
i = -1;
j = 1;
break;
}
case SOUTH_EAST:
i = 1;
j = 1;
break;
case NORTH_WEST:
i = -1;
j = -1;
break;
case SOUTH_WEST:
i = 1;
j = -1;
break;
}
int c = 1;
while (counter > 1) {
if (wordAsChars[c] != Data.copyCrossword[y + i][x + j]) return;
x += j;
y += i;
counter--;
c++;
}
tempWord = new Word(String.valueOf(wordAsChars));
tempWord.setStartPoint(currentWord.startX,currentWord.startY);
tempWord.setEndPoint(x,y);
Data.result.add(tempWord);
}
public enum Direction {
EAST, WEST, NORTH, SOUTH, NORTH_EAST, SOUTH_EAST, NORTH_WEST, SOUTH_WEST
}
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);
}
}
}