Выводит все слова. Может его не устраивает, что если много одинаковых слов, то я выведу только одно слово на одну проверку? Но об этом в условиях не сказано... Алгоритм находит даже 1 букву. По диагоналям получился слишком мудрёный код.
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', 'w'}
};
int[][] crossword1 = new int[][]{
{'f', 'd', 'e', 'r'},
{'u', 'g', 'a', 'm'},
{'l', 'n', 'g', 'r'},
{'n', 'l', 'p', 'w'}
};
System.out.println(detectAllWords(crossword, "home", "same"));
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
List<Word> wordList = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
if (rowFounder(crossword, words[i]) != null)
wordList.add(rowFounder(crossword, words[i])); // сделано целиком!
if (colomnFounder(crossword, words[i]) != null)
wordList.add(colomnFounder(crossword, words[i])); // сделано целиком!
if (diagonalLRFounder(crossword, words[i]) != null)
wordList.add(diagonalLRFounder(crossword, words[i])); //сделано целиком!
if (diagonalRLFounder(crossword, words[i]) != null) wordList.add(diagonalRLFounder(crossword, words[i]));
}
return wordList;
}
public static Word rowFounder(int[][] crossword, String word) {
int startX = 0;
int startY = 0;
StringBuffer sb = new StringBuffer();
Word wordRet;
int wor = 0;
for (int i = 0; i < crossword.length; i++) {
for (int j = 0; j < crossword[i].length; j++) {
if (crossword[i][j] == word.charAt(wor)) {
sb.append((char) crossword[i][j]);
if (wor == 0) {
startX = j;
startY = i;
}
if (sb.toString().equals(word)) {
wordRet = new Word(sb.toString());
wordRet.setStartPoint(startX, startY);
wordRet.setEndPoint(j, i);
return wordRet;
}
++wor;
} else {
sb.delete(0, sb.length());
wor = 0;
}
}
sb.delete(0, sb.length());
wor = 0;
}
for (int i = 0; i < crossword.length; i++) {
for (int j = crossword[i].length - 1; j >= 0; j--) {
if (crossword[i][j] == word.charAt(wor)) {
sb.append((char) crossword[i][j]);
if (wor == 0) {
startX = j;
startY = i;
}
if (sb.toString().equals(word)) {
wordRet = new Word(sb.toString());
wordRet.setStartPoint(startX, startY);
wordRet.setEndPoint(j, i);
return wordRet;
}
++wor;
} else {
sb.delete(0, sb.length());
wor = 0;
}
}
sb.delete(0, sb.length());
wor = 0;
}
return null;
}
public static Word colomnFounder(int[][] crossword, String word) {
int startX = 0;
int startY = 0;
StringBuffer sb = new StringBuffer();
Word wordRet;
int wor = 0;
for (int i = 0; i < crossword[0].length; i++) {
for (int j = 0; j < crossword.length; j++) {
if (crossword[j][i] == word.charAt(wor)) {
sb.append((char) crossword[j][i]);
if (wor == 0) {
startX = i;
startY = j;
}
if (sb.toString().equals(word)) {
wordRet = new Word(sb.toString());
wordRet.setStartPoint(startX, startY);
wordRet.setEndPoint(i, j);
return wordRet;
}
++wor;
} else {
sb.delete(0, sb.length());
wor = 0;
}
}
sb.delete(0, sb.length());
wor = 0;
}
for (int i = 0; i < crossword[0].length; i++) {
for (int j = crossword.length - 1; j >= 0; j--) {
if (crossword[j][i] == word.charAt(wor)) {
sb.append((char) crossword[j][i]);
if (wor == 0) {
startX = i;
startY = j;
}
if (sb.toString().equals(word)) {
wordRet = new Word(sb.toString());
wordRet.setStartPoint(startX, startY);
wordRet.setEndPoint(i, j);
return wordRet;
}
++wor;
} else {
sb.delete(0, sb.length());
wor = 0;
}
}
sb.delete(0, sb.length());
wor = 0;
}
return null;
}
public static Word diagonalLRFounder(int[][] crossword, String word) {
int startX = 0;
int startY = 0;
StringBuffer sb = new StringBuffer();
Word wordRet;
int wor = 0;
int i = crossword.length - 1;
int j = 0;
int d = 0;
boolean reverse = false;
while (true) {
if (!reverse) {
if (crossword[i + j][j + d] == word.charAt(wor)) {
sb.append((char) crossword[i + j][j + d]);
if (wor == 0) {
startX = j + d;
startY = i + j;
}
if (sb.toString().equals(word)) {
wordRet = new Word(sb.toString());
wordRet.setStartPoint(startX, startY);
wordRet.setEndPoint(j + d, i + j);
return wordRet;
} else wor++;
} else {
sb.delete(0, sb.length());
wor = 0;
if (crossword[i + j][j + d] == word.charAt(wor)) {
sb.append((char) crossword[i + j][j + d]);
startX = j + d;
startY = i + j;
wor++;
}
}
if (i > 0) {
if (j < crossword.length - i - 1) j++;
else {
sb.delete(0, sb.length());
wor = 0;
j = 0;
i--;
}
} else {
if (d == 0) {
if (j < crossword.length - 1) j++;
else d++;
}
if (j < crossword.length - d && d != 0) j++;
else {
d++;
j = 0;
if (d > crossword.length - 1) {
i = crossword.length - 1;
d = 0;
reverse = true;
}
}
}
} else {
if (crossword[i + j][j + d] == word.charAt(wor)) {
sb.append((char) crossword[i + j][j + d]);
if (wor == 0) {
startX = j + d;
startY = i + j;
}
if (sb.toString().equals(word)) {
wordRet = new Word(sb.toString());
wordRet.setStartPoint(startX, startY);
wordRet.setEndPoint(j + d, i + j);
return wordRet;
} else wor++;
} else {
sb.delete(0, sb.length());
wor = 0;
if (crossword[i + j][j + d] == word.charAt(wor)) {
sb.append((char) crossword[i + j][j + d]);
startX = j + d;
startY = i + j;
wor++;
}
}
if (crossword.length != crossword[0].length) {
if (d < crossword.length) {
if (j != -d) j--;
else {
d++;
sb.delete(0, sb.length());
wor = 0;
j = 0;
}
} else {
if (j > -i) {
j--;
} else {
i--;
if (i == -1) {
break;
}
j = 0;
}
}
} else {
if (d < crossword.length - 1) {
if (j != -d) j--;
else {
d++;
sb.delete(0, sb.length());
wor = 0;
j = 0;
}
} else {
if (j > -i) {
j--;
} else {
i--;
if (i == -1) {
break;
}
j = 0;
}
}
}
}
}
return null;
}
public static Word diagonalRLFounder(int[][] crossword, String word) {
int startX = 0;
int startY = 0;
StringBuffer sb = new StringBuffer();
Word wordRet;
int wor = 0;
int i = 0;
int j = 0;
int d = 0;
boolean reverse = false;
while (true) {
if (!reverse) {
if (crossword[j + d][i - j] == word.charAt(wor)) {
sb.append((char) crossword[j + d][i - j]);
if (wor == 0) {
startY = j + d;
startX = i - j;
}
if (sb.toString().equals(word)) {
wordRet = new Word(sb.toString());
wordRet.setStartPoint(startX, startY);
wordRet.setEndPoint(i - j, j + d);
return wordRet;
} else wor++;
} else {
sb.delete(0, sb.length());
wor = 0;
if (crossword[j + d][i - j] == word.charAt(wor)) {
sb.append((char) crossword[j + d][i - j]);
startY = j + d;
startX = i - j;
wor++;
}
}
if (i < crossword.length - 1) {
if (j != i && (i + 1) >= word.length()) j++;
else {
sb.delete(0, sb.length());
wor = 0;
j = 0;
i++;
}
} else if (i < crossword[0].length - 1 && crossword.length != crossword[0].length) {
if (j < crossword.length - 1) j++;
else {
sb.delete(0, sb.length());
wor = 0;
i++;
j = 0;
}
} else {
if (i == crossword[0].length - 1 && d <= crossword.length - 1) {
if (j + d < crossword.length - 1 && (crossword.length - d) >= word.length()) j++;
else {
d++;
sb.delete(0, sb.length());
wor = 0;
j = 0;
if (d > crossword.length - 1) {
i = 0;
d = 0;
reverse = true;
}
}
}
}
} else {
if (crossword[i - j][j + d] == word.charAt(wor)) {
sb.append((char) crossword[i - j][j + d]);
if (wor == 0) {
startX = j + d;
startY = i - j;
}
if (sb.toString().equals(word)) {
wordRet = new Word(sb.toString());
wordRet.setStartPoint(startX, startY);
wordRet.setEndPoint(j + d, i - j);
return wordRet;
} else wor++;
} else {
sb.delete(0, sb.length());
wor = 0;
if (crossword[i - j][j + d] == word.charAt(wor)) {
sb.append((char) crossword[i - j][j + d]);
startX = j + d;
startY = i - j;
wor++;
}
}
if (i == crossword.length - 1 & j == crossword.length - 1) {
d++;
j = -1;
wor = 0;
sb.delete(0, sb.length());
} // !!!посмотреть как будет работать с любыми кроссвордами это условие
if (i <= crossword.length - 1 & d == 0) {
if (j != i && (i + 1) >= word.length()) j++;
else {
sb.delete(0, sb.length());
wor = 0;
j = 0;
i++;
}
} else {
if (crossword.length != crossword[0].length) {
if ((j + d) <= crossword.length - 1) {
j++;
} else {
d++;
sb.delete(0, sb.length());
wor = 0;
if (d > crossword.length - 1) return null;
j = 0;
}
} else {
if ((j + d) < crossword.length - 1) {
j++;
} else {
d++;
sb.delete(0, sb.length());
wor = 0;
if (d > crossword.length - 1) return null;
j = 0;
}
}
}
}
}
}
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);
}
}
}