Skip to content

Instantly share code, notes, and snippets.

@Shtaba09
Created September 28, 2018 21:29
Show Gist options
  • Save Shtaba09/e135195050fb3c28f4f84845906b38d3 to your computer and use it in GitHub Desktop.
Save Shtaba09/e135195050fb3c28f4f84845906b38d3 to your computer and use it in GitHub Desktop.
Идеальное решение
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', 'o', 's', 'o', 's'},
{'l', 'o', 'o', 'o', 'o', 'o'},
{'m', 's', 'o', 's', 'o', 's'},
{'p', 'o', 'e', 'e', 'j', 'j'}
};
List words = (detectAllWords(crossword, "sos"));
for (int i = 0; i <words.size() ; i++) {
System.out.println(words.get(i));
}
System.out.println(words.size());
/*
Ожидаемый результат
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] crossword, String... words) {
int[][] crossword1 = crossword.clone();
for(int i=0;i<crossword.length; i++){
crossword1[i]=crossword[i].clone();
}
List<Word> wordslist = new ArrayList<Word>();
for (String word : words){
char[] txtch = word.toCharArray();
int symbol =txtch[0];
System.out.println("Step 1 get symbol 0 is:"+symbol );
for (int x=0; x<crossword1.length; x++){
System.out.println("Step 2 search coordinate start "+String.valueOf(txtch[0])+ " in x" + x );
for (int y=0;y<crossword[x].length;y++){
if(crossword1[x][y]==symbol){
System.out.println("Start symbol searched "+ x + " "+ y + ".Start search next and validate obj ");
List<Word> wordobj = checkWord(word,crossword1,txtch,x,y);
wordslist.addAll(wordobj);
}
}
}
}
return wordslist;
}
public static List<Word> checkWord (String word, int [][] crossword1, char [] txtch, int x, int y ){
List<Word> ws =new ArrayList<>();
Word objWord = new Word(word);
for(int j=0;j<8;j++){
int matchcount=0;
switch (j) {
case 0:
for (int i=0;i<txtch.length;i++){
try {if(crossword1[x+i][y]==txtch[i]){matchcount++;}}
catch (ArrayIndexOutOfBoundsException e){}
}
if (matchcount==txtch.length){
objWord = new Word(word);
objWord.setStartPoint(y, x);
objWord.setEndPoint(y,x+word.length()-1);
System.out.println(word+" it is vertical down");
ws.add(objWord);}
break;
case 1:
for (int i=0;i<txtch.length;i++){
try {if(crossword1[x][y-i]==txtch[i]){matchcount++;}}
catch (ArrayIndexOutOfBoundsException e){}
}
if (matchcount==txtch.length){
objWord = new Word(word);
objWord.setStartPoint(y, x);
objWord.setEndPoint(y-word.length()+1,x);
System.out.println(word+" it is horizontal<");
ws.add(objWord);}
break;
case 2:
for (int i=0;i<txtch.length;i++){
try {if(crossword1[x][y+i]==txtch[i]){matchcount++;}}
catch (ArrayIndexOutOfBoundsException e){}
}
if (matchcount==txtch.length){
objWord = new Word(word);
objWord.setStartPoint(y, x);
objWord.setEndPoint(y+word.length()-1, x);
System.out.println(word+" it is horizontal>");
ws.add(objWord);}
break;
case 3:
for (int i=0;i<txtch.length;i++){
try {if(crossword1[x-i][y-i]==txtch[i]){matchcount++;}}
catch (ArrayIndexOutOfBoundsException e){}
}
if (matchcount==txtch.length){
objWord = new Word(word);
objWord.setStartPoint(y,x);
objWord.setEndPoint(y-word.length()+1,x-word.length()+1);
System.out.println(word+" it is diagonal up left");
ws.add(objWord);}
break;
case 4:
for (int i=0;i<txtch.length;i++){
try {if(crossword1[x+i][y-i]==txtch[i]){matchcount++;}}
catch (ArrayIndexOutOfBoundsException e){}
}
if (matchcount==txtch.length) {
objWord = new Word(word);
objWord.setStartPoint(y, x);
objWord.setEndPoint(y-word.length()+1,x+word.length()-1);
System.out.println(word+" it is diagonal down left");
ws.add(objWord);}
break;
case 5:
for (int i=0;i<txtch.length;i++){
try {if(crossword1[x-i][y]==txtch[i]){matchcount++;}}
catch (ArrayIndexOutOfBoundsException e){}
}
if (matchcount==txtch.length){
objWord = new Word(word);
objWord.setStartPoint(y, x);
objWord.setEndPoint(y,x-word.length()+1);
System.out.println(word+" it is vertical up");
ws.add(objWord);}
break;
case 6:
for (int i=0;i<txtch.length;i++){
try {if(crossword1[x+i][y+i]==txtch[i]){matchcount++;}}
catch (ArrayIndexOutOfBoundsException e){}
}
if (matchcount==txtch.length){
objWord = new Word(word);
objWord.setStartPoint(y, x);
objWord.setEndPoint(y+word.length()-1,x+word.length()-1);
System.out.println(word+" it is diagonal down right");
ws.add(objWord);}
break;
case 7:
for (int i=0;i<txtch.length;i++){
try {if(crossword1[x-i][y+i]==txtch[i]){matchcount++;}}
catch (ArrayIndexOutOfBoundsException e){}
}
if (matchcount==txtch.length) {
objWord = new Word(word);
objWord.setStartPoint(y, x);
objWord.setEndPoint( y+word.length()-1,x-word.length()+1);
System.out.println(word+" it is diagonal up right ");
ws.add(objWord);}
break;
}
}
return ws ;
}
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) {
startY = j;
startX = i;
}
public void setEndPoint(int i, int j) {
endY = j;
endX = i;
}
@Override
public String toString() {
return String.format("%s - (%d, %d) - (%d, %d)", text, startX, startY, endX, endY);
}
}
}
taskKey="com.javarush.task.task20.task2027"
Кроссворд
1. Дан двумерный массив, который содержит буквы английского алфавита в нижнем регистре.
2. Метод detectAllWords должен найти все слова из words в массиве crossword.
3. Элемент(startX, startY) должен соответствовать первой букве слова, элемент(endX, endY) - последней.
text - это само слово, располагается между начальным и конечным элементами
4. Все слова есть в массиве.
5. Слова могут быть расположены горизонтально, вертикально и по диагонали как в нормальном, так и в обратном порядке.
6. Метод main не участвует в тестировании.
Требования:
1. В классе Solution должен существовать метод detectAllWords.
2. В классе Solution должен существовать статический класс Word.
3. Класс Solution не должен содержать статические поля.
4. Метод detectAllWords должен быть статическим.
5. Метод detectAllWords должен быть публичным.
6. Метод detectAllWords должен возвращать список всех слов в кроссворде (согласно условию задачи).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment