Repository navigation
Expand file tree
/
Copy pathSolution.java
More file actions
88 lines (77 loc) · 2.79 KB
/
Copy pathSolution.java
File metadata and controls
88 lines (77 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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', 'j'}
};
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<>();
int[] dx = new int[] { 0, 1, 1, 1, 0, -1, -1, -1};
int[] dy = new int[] {-1, -1, 0, 1, 1, 1, 0, -1};
for (String s : words) {
char[] c = s.toCharArray();
for (int y = 0; y < crossword.length; y++) {
for (int x = 0; x < crossword[y].length; x++) {
if (crossword[y][x] == c[0]) {
for (int i = 0; i < dx.length; i++) {
boolean wordFound = true;
for (int j = 1; j < c.length; j++) {
if (y + dy[i] * j < 0 || y + dy[i] * j >= crossword.length ||
x + dx[i] * j < 0 || x + dx[i] * j >= crossword[y + dy[i] * j].length ||
crossword[y + dy[i] * j][x + dx[i] * j] != c[j]) {
wordFound = false;
break;
}
}
if (wordFound) {
Word w = new Word(s);
w.setStartPoint(x, y);
w.setEndPoint(x + dx[i] * (c.length - 1), y + dy[i] * (c.length - 1));
wordList.add(w);
}
}
}
}
}
}
return wordList;
}
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);
}
}
}