Итак, все тесты из предыдущих вопросов протыкал - все работает. Но валидатор не принимает. Может я условия не понимаю до конца?
Матрица:
{'f', 'd', 'e', 'r', 'l', 'k'},
{'u', 's', 'a', 'm', 'e', 'o'},
{'l', 'u', 'g', 'r', 'o', 'v'},
{'m', 'l', 'p', 'r', 'r', 'h'},
{'p', 'o', 'e', 'p', 'j', 'j'},
{'t', 'r', 'w', 'e', 'k', 'a'},
{'m', 'c', 'n', 'q', 's', 'o'},
{'z', 'o', 'e', 'f', 'h', 's'}
Тесты:
detectAllWords(crossword, "rrm","prr", "rrad","s", "eroo","muar","jroel","leo","same","home","fderlk","kovhj","jjpeop");
Вывод:
rrm - (3, 3) - (3, 1)
prr - (2, 3) - (4, 3)
prr - (3, 4) - (3, 2)
rrad - (4, 3) - (1, 0)
s - (1, 1) - (1, 1)
s - (4, 6) - (4, 6)
s - (5, 7) - (5, 7)
eroo - (2, 4) - (5, 1)
muar - (0, 3) - (3, 0)
jroel - (4, 4) - (4, 0)
leo - (4, 0) - (4, 2)
same - (1, 1) - (4, 1)
home - (5, 3) - (2, 0)
fderlk - (0, 0) - (5, 0)
kovhj - (5, 0) - (5, 4)
jjpeop - (0, 4) - (5, 4)
Последнюю версию кода прикрепил.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', 'u', 'g', 'r', 'o', 'v'},
{'m', 'l', 'p', 'r', 'r', 'h'},
{'p', 'o', 'e', 'p', 'j', 'j'},
{'t', 'r', 'w', 'e', 'k', 'a'},
{'m', 'c', 'n', 'q', 's', 'o'},
{'z', 'o', 'e', 'f', 'h', 's'}
};
detectAllWords(crossword, "rrm","prr", "rrad","s", "eroo","muar","jroel","leo","same","home","fderlk","kovhj","jjpeop");
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
List<Word> list = new ArrayList<Word>();
for (String str:words) {
for(int i=0; i<crossword.length; i++){
for(int j=0; j<crossword[i].length; j++){
if(crossword[i][j]==str.toCharArray()[0]){
//Слово справа
Word word = getRightWord(i,j,crossword, str);
if(addWords(list, word)==-1)break;
//Слово слева
word = getLeftWord(i,j,crossword, str);
if(addWords(list, word)==-1)break;
//Слово Вверх
word = getTopWord(i,j,crossword, str);
addWords(list, word);
//Слово Вниз
word = getBottomWord(i,j,crossword, str);
if(addWords(list, word)==-1)break;
//Слово слева вверх
word = getLeftTopWord(i,j,crossword,str);
if(addWords(list, word)==-1)break;
//Слово слева вниз
word = getLeftBottomWord(i,j,crossword,str);
if(addWords(list, word)==-1)break;
//Слово справа вверх
word = getRightTopWord(i,j,crossword,str);
if(addWords(list, word)==-1)break;
//Слово справа вниз
word = getRightBottomWord(i,j,crossword,str);
if(addWords(list, word)==-1)break;
}
}
}
}
for (Word w:list
) {
System.out.println(w.toString());
}
return list;
}
private static int addWords(List<Word> list, Word word){
if(word!=null){
for (Word w:list) {
if(w.toString().equals(word.toString()))return -1;
}
list.add(word);
return 0;
}
return -2;
}
private static Word getLeftWord(int i, int j, int[][] crossword, String str) {
int jEnd=j;
char[] charArr = str.toCharArray();
for (int charI=1; charI<charArr.length;charI++) {
int chR = charLeft(i,jEnd,crossword);
if(chR==-1||chR!=charArr[charI])return null;
jEnd--;
}
Word word = new Word(str);
word.setEndPoint(j,i);
word.setStartPoint(jEnd,i);
return word;
}
private static Word getRightWord(int i, int j, int[][] crossword, String str) {
int jEnd=j;
char[] charArr = str.toCharArray();
for (int charI=1; charI<charArr.length;charI++) {
int chR = charRight(i,jEnd,crossword);
if(chR==-1||chR!=charArr[charI])return null;
jEnd++;
}
Word word = new Word(str);
word.setStartPoint(j,i);
word.setEndPoint(jEnd,i);
return word;
}
private static Word getTopWord(int i, int j, int[][] crossword, String str) {
int iEnd=i;
char[] charArr = str.toCharArray();
for (int charJ=1; charJ<charArr.length;charJ++) {
int chR = charTop(iEnd,j,crossword);
if(chR==-1||chR!=charArr[charJ])return null;
iEnd--;
}
Word word = new Word(str);
word.setStartPoint(j,i);
word.setEndPoint(j,iEnd);
return word;
}
private static Word getBottomWord(int i, int j, int[][] crossword, String str) {
int iEnd=i;
char[] charArr = str.toCharArray();
for (int charJ=1; charJ<charArr.length;charJ++) {
int chR = charBottom(iEnd,j,crossword);
if(chR==-1||chR!=charArr[charJ])return null;
iEnd++;
}
Word word = new Word(str);
word.setStartPoint(j,i);
word.setEndPoint(j,iEnd);
return word;
}
private static Word getLeftTopWord(int i, int j, int[][] crossword, String str) {
int jEnd=j;
int iEnd = i;
char[] charArr = str.toCharArray();
for (int charI=1; charI<charArr.length;charI++) {
int chR = charLeftTop(iEnd,jEnd,crossword);
if(chR==-1||chR!=charArr[charI])return null;
jEnd--;
iEnd--;
}
Word word = new Word(str);
word.setStartPoint(j,i);
word.setEndPoint(jEnd,iEnd);
return word;
}
private static Word getRightTopWord(int i, int j, int[][] crossword, String str) {
int jEnd=j;
int iEnd = i;
char[] charArr = str.toCharArray();
for (int charI=1; charI<charArr.length;charI++) {
int chR = charRightTop(iEnd,jEnd,crossword);
if(chR==-1||chR!=charArr[charI])return null;
jEnd++;
iEnd--;
}
Word word = new Word(str);
word.setStartPoint(j,i);
word.setEndPoint(jEnd,iEnd);
return word;
}
private static Word getRightBottomWord(int i, int j, int[][] crossword, String str) {
int jEnd=j;
int iEnd = i;
char[] charArr = str.toCharArray();
for (int charI=1; charI<charArr.length;charI++) {
int chR = charRightBottom(iEnd,jEnd,crossword);
if(chR==-1||chR!=charArr[charI])return null;
jEnd++;
iEnd++;
}
Word word = new Word(str);
word.setStartPoint(j,i);
word.setEndPoint(jEnd,iEnd);
return word;
}
private static Word getLeftBottomWord(int i, int j, int[][] crossword, String str) {
int jEnd=j;
int iEnd = i;
char[] charArr = str.toCharArray();
for (int charI=1; charI<charArr.length;charI++) {
int chR = charLeftBottom(iEnd,jEnd,crossword);
if(chR==-1||chR!=charArr[charI])return null;
jEnd--;
iEnd++;
}
Word word = new Word(str);
word.setStartPoint(j,i);
word.setEndPoint(jEnd,iEnd);
return word;
}
public static int charRight(int i, int j, int[][] crossword){
int nextJ = j<crossword[i].length-1?j+1:j;
if(j==nextJ)return -1;
return crossword[i][nextJ];
}
public static int charLeft(int i,int j, int[][] crossword){
int beforeJ = j>0?j-1:j;
if(j==beforeJ)return -1;
return crossword[i][beforeJ];
}
public static int charBottom(int i,int j, int[][] crossword){
int nextI = i<crossword.length-1?i+1:i;
if(i==nextI)return -1;
return crossword[nextI][j];
}
public static int charTop(int i,int j, int[][] crossword){
int beforeI = i>0?i-1:i;
if(i==beforeI)return -1;
return crossword[beforeI][j];
}
public static int charLeftTop(int i,int j, int[][] crossword){
int beforeJ = j>0?j-1:j;
int beforeI = i>0?i-1:i;
if(j==beforeJ)return -1;
if(i==beforeI)return -1;
return crossword[beforeI][beforeJ];
}
public static int charRightTop(int i,int j, int[][] crossword){
int nextJ = j<crossword[i].length-1?j+1:j;
int beforeI = i>0?i-1:i;
if(j==nextJ)return -1;
if(i==beforeI)return -1;
return crossword[beforeI][nextJ];
}
public static int charRightBottom(int i,int j, int[][] crossword){
int nextJ = j<crossword[i].length-1?j+1:j;
int nextI = i<crossword.length-1?i+1:i;
if(j==nextJ)return -1;
if(i==nextI)return -1;
return crossword[nextI][nextJ];
}
public static int charLeftBottom(int i,int j, int[][] crossword){
int beforeJ = j>0?j-1:j;
int nextI = i<crossword.length-1?i+1:i;
if(j==beforeJ)return -1;
if(i==nextI)return -1;
return crossword[nextI][beforeJ];
}
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);
}
}
}