Пусть и коряво, но написал +/- работающий год, на тестах из условия и моих самописных все ок, однако валидатор не принимает с пометкой, что не все слова найдены.
Буду очень благодарен, тому, кто укажет на ошибку или даст тест, который программа не отработает
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', 'a'}
};
detectAllWords(crossword, "a").forEach(System.out::println);
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
List<Word> res = new ArrayList<>();
for (String word : words) {
for (int i = 0; i < crossword.length; i++) {
for (int j = 0; j < crossword[0].length; j++) {
Word result;
result = detectColPlus(crossword,i,j,word);
if (result!=null) {
res.add(result);
}
result = detectColMinus(crossword,i,j,word);
if (result!=null && word.length()>1) {
res.add(result);
}
result = detectRowMinus(crossword,i,j,word);
if (result!=null && word.length()>1) {
res.add(result);
}
result = detectRowPlus(crossword,i,j,word);
if (result!=null && word.length()>1) {
res.add(result);
}
result = detectRowPlusColPlus(crossword,i,j,word);
if (result!=null && word.length()>1) {
res.add(result);
}
result = detectRowPlusColMinus(crossword,i,j,word);
if (result!=null && word.length()>1) {
res.add(result);
}
result = detectRowMinusColMinus(crossword,i,j,word);
if (result!=null && word.length()>1) {
res.add(result);
}
result = detectRowMinusColPlus(crossword,i,j,word);
if (result!=null && word.length()>1) {
res.add(result);
}
}
}
}
return res;
}
public static Word detectColPlus(int[][] crossword, int row, int col, String word) {
StringBuilder builder = new StringBuilder();
for (int i = col; i < crossword.length; i++) {
builder.append((char)(crossword[row][i]));
if (builder.toString().equals(word)) {
Word newWord = new Word(word);
newWord.setStartPoint(col,row);
newWord.setEndPoint(i,row);
return newWord;
}
}
return null;
}
public static Word detectRowPlus(int[][] crossword, int row, int col, String word) {
StringBuilder builder = new StringBuilder();
for (int i = row; i < crossword.length; i++) {
builder.append((char)(crossword[i][col]));
if (builder.toString().equals(word)) {
Word newWord = new Word(word);
newWord.setStartPoint(col,row);
newWord.setEndPoint(col,i);
return newWord;
}
}
return null;
}
public static Word detectColMinus(int[][] crossword, int row, int col, String word) {
StringBuilder builder = new StringBuilder();
for (int i = col; i >= 0; i--) {
builder.append((char)(crossword[row][i]));
if (builder.toString().equals(word)) {
Word newWord = new Word(word);
newWord.setStartPoint(col,row);
newWord.setEndPoint(i,row);
return newWord;
}
}
return null;
}
public static Word detectRowMinus(int[][] crossword, int row, int col, String word) {
StringBuilder builder = new StringBuilder();
for (int i = row; i >= 0; i--) {
builder.append((char)(crossword[i][col]));
if (builder.toString().equals(word)) {
Word newWord = new Word(word);
newWord.setStartPoint(col,row);
newWord.setEndPoint(col,i);
return newWord;
}
}
return null;
}
public static Word detectRowPlusColPlus(int[][] crossword, int row, int col, String word) {
StringBuilder builder = new StringBuilder();
for(int i=row, j=col; i < crossword.length && j < crossword[i].length; i++, j++) {
builder.append((char)(crossword[i][j]));
if (builder.toString().equals(word)) {
Word newWord = new Word(word);
newWord.setStartPoint(col,row);
newWord.setEndPoint(j,i);
return newWord;
}
}
return null;
}
public static Word detectRowPlusColMinus(int[][] crossword, int row, int col, String word) {
StringBuilder builder = new StringBuilder();
for(int i=row, j=col; i < crossword.length && j >= 0; i++, j--) {
builder.append((char)(crossword[i][j]));
if (builder.toString().equals(word)) {
Word newWord = new Word(word);
newWord.setStartPoint(col,row);
newWord.setEndPoint(j,i);
return newWord;
}
}
return null;
}
public static Word detectRowMinusColMinus(int[][] crossword, int row, int col, String word) {
StringBuilder builder = new StringBuilder();
for(int i=row, j=col; i >=0 && j >= 0; i--, j--) {
builder.append((char)(crossword[i][j]));
if (builder.toString().equals(word)) {
Word newWord = new Word(word);
newWord.setStartPoint(col,row);
newWord.setEndPoint(j,i);
return newWord;
}
}
return null;
}
public static Word detectRowMinusColPlus(int[][] crossword, int row, int col, String word) {
StringBuilder builder = new StringBuilder();
for(int i=row, j=col; i >= 0 && j < crossword[i].length; i--, j++) {
builder.append((char)(crossword[i][j]));
if (builder.toString().equals(word)) {
Word newWord = new Word(word);
newWord.setStartPoint(col,row);
newWord.setEndPoint(j,i);
return newWord;
}
}
return null;
}
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);
}
}
}