Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.task.task20.task2027;
- import java.util.ArrayList;
- import java.util.List;
- /*
- Кроссворд
- */
- public class Solution {
- public static void main(String[] args) {
- int[][] crossword2 = new int[][]{
- {'r', 'm', 'a', 'r', 'r', 'e'},
- {'m', 'e', 'a', 'e', 'e', 'm'},
- {'s', 'a', 'm', 'e', 's', 'o'},
- {'m', 'o', 'p', 'o', 'o', 'h'},
- {'h', 'r', 'e', 'm', 'h', 'h'}
- };
- detectAllWords(crossword2, "home", "same", "homer", "r").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> result = new ArrayList<>();
- for (String s : words) {
- char [] sDisassembled = s.toCharArray();
- char firstChar = sDisassembled[0];
- ArrayList<Integer[]> possibleStarts = new ArrayList<>();
- for (int y = 0; y < crossword.length; y++) {
- for (int x = 0; x < crossword[y].length; x++) {
- if (crossword[y][x] == firstChar)
- possibleStarts.add(new Integer[]{y, x});
- }
- }
- //
- for (Integer[] startCoords : possibleStarts) {
- if (s.length() > 1) {
- int[] neighbors = getNeighbors(crossword, startCoords[0], startCoords[1]);
- for (int i = 0; i < 8; i++) {
- if (neighbors[i] == sDisassembled[1]) {
- CheckingString checkingString = getWordByDirection(crossword, i, startCoords[0], startCoords[1], s.length() - 1);
- if (s.equals(checkingString.getValue())) {
- Word currentWord = new Word(s);
- currentWord.setStartPoint(startCoords[1], startCoords[0]);
- currentWord.setEndPoint(checkingString.getxPos(), checkingString.getyPos());
- result.add(currentWord);
- }
- }
- }
- } else {
- Word currentWord = new Word(s);
- currentWord.setStartPoint(startCoords[1], startCoords[0]);
- currentWord.setEndPoint(startCoords[1], startCoords[0]);
- result.add(currentWord);
- }
- }
- }
- return result;
- }
- public static CheckingString getWordByDirection(int[][] crossword, int direction, int yPos, int xPos, int charCount) {
- String result = "" + (char) crossword[yPos][xPos];
- switch (direction) {
- case 0 : {
- if (charCount <= yPos) {
- while (charCount > 0) {
- yPos--;
- result += (char) crossword[yPos][xPos];
- charCount--;
- }
- }
- break;
- }
- case 1 : {
- if ((charCount <= yPos) && (charCount < crossword[yPos].length - xPos)) {
- while (charCount > 0) {
- yPos--;
- xPos++;
- result += (char) crossword[yPos][xPos];
- charCount--;
- }
- }
- break;
- }
- case 2 : {
- if (charCount < crossword[yPos].length - xPos) {
- while (charCount > 0) {
- xPos++;
- result += (char) crossword[yPos][xPos];
- charCount--;
- }
- }
- break;
- }
- case 3 : {
- if ((charCount < crossword[yPos].length - xPos) && (charCount < crossword.length - yPos)) {
- while (charCount > 0) {
- yPos++;
- xPos++;
- result += (char) crossword[yPos][xPos];
- charCount--;
- }
- }
- break;
- }
- case 4 : {
- if (charCount < crossword.length - yPos) {
- while (charCount > 0) {
- yPos++;
- result += (char) crossword[yPos][xPos];
- charCount--;
- }
- }
- break;
- }
- case 5 : {
- if ((charCount < crossword.length - yPos) && (charCount < xPos)) {
- while (charCount > 0) {
- yPos++;
- xPos--;
- result += (char) crossword[yPos][xPos];
- charCount--;
- }
- }
- break;
- }
- case 6 : {
- if (charCount <= xPos) {
- while (charCount > 0) {
- xPos--;
- result += (char) crossword[yPos][xPos];
- charCount--;
- }
- }
- break;
- }
- case 7 : {
- if ((charCount <= yPos) && (charCount <= xPos)) {
- while (charCount > 0) {
- yPos--;
- xPos--;
- result += (char) crossword[yPos][xPos];
- charCount--;
- }
- }
- break;
- }
- }
- return new CheckingString(yPos, xPos, result);
- }
- public static int[] getNeighbors(int[][] crossword, int yPos, int xPos) {
- int [] result = new int[] {'.', '.', '.', '.', '.', '.', '.', '.'};
- // 0 = up, 1 = upRight, 2 = right, 3 = downRight, 4 = down, 5 = downLeft, 6 = left, 7 = upLeft
- if (yPos > 0) {
- result[0] = crossword[yPos - 1][xPos]; // up
- if (xPos < crossword[yPos].length - 1)
- result[1] = crossword[yPos - 1][xPos + 1]; // upRight
- if (xPos > 0) result[7] = crossword[yPos - 1][xPos - 1]; // upLeft
- }
- //
- if (xPos < crossword[yPos].length - 1) {
- result[2] = crossword[yPos][xPos + 1]; // right
- if (yPos < crossword.length - 1)
- result[3] = crossword[yPos + 1][xPos + 1]; //downRight
- }
- //
- if (yPos < crossword.length - 1) {
- result[4] = crossword[yPos + 1][xPos]; // down
- if (xPos > 0) result[5] = crossword[yPos + 1][xPos - 1]; // downLeft
- }
- //
- if (xPos > 0) result[6] = crossword[yPos][xPos - 1]; // left
- //
- return result;
- }
- public static class CheckingString {
- private int xPos;
- private int yPos;
- private String value;
- public CheckingString(int yPosArg, int xPosArg, String valueArg) {
- this.value = valueArg;
- this.xPos = xPosArg;
- this.yPos = yPosArg;
- }
- public int getxPos() {
- return xPos;
- }
- public int getyPos() {
- return yPos;
- }
- public String getValue() {
- return value;
- }
- }
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement