-
Notifications
You must be signed in to change notification settings - Fork 4
/
WheresWaldorf.java
91 lines (82 loc) · 3.04 KB
/
WheresWaldorf.java
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
89
90
91
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class WheresWaldorf {
private static final BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
private final int n;
private final int m;
private final char[][] table;
public WheresWaldorf(int n, int m, char[][] table) {
this.n = n;
this.m = m;
this.table = table;
}
private static final int[][] dir = new int[][] {
{ 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 }, { 1, 1 }, { -1, -1 },
{ -1, 1 }, { 1, -1 }
};
public int[] find(String word) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
for (int k = 0; k < dir.length; ++k) {
if (check(dir[k][0], dir[k][1], i, j, word)) {
return new int[] { i + 1, j + 1 };
}
}
}
}
return null;
}
private boolean check(int di, int dj, int i, int j, String word) {
if (word.length() == 1) {
return table[i][j] == word.charAt(0);
}
int pos = 0;
while (i >= 0 && i < n && j >= 0 && j < m && pos < word.length() &&
table[i][j] == word.charAt(pos)) {
j += dj;
i += di;
pos++;
}
return pos == word.length();
}
private static String toString(int[] arr) {
return Arrays.stream(arr).mapToObj(String::valueOf)
.collect(Collectors.joining(" "));
}
public static void main(String[] args) throws IOException {
int cases = Integer.parseInt(reader.readLine().trim());
reader.readLine();
for (int k = 0; k < cases; ++k) {
List<Integer> nm = stream(reader.readLine().trim().split(" "))
.filter(x -> !x.equals("")).map(Integer::parseInt)
.collect(toList());
char[][] table = new char[nm.get(0)][nm.get(1)];
for (int i = 0; i < nm.get(0); ++i) {
String currentLine = reader.readLine();
for (int j = 0; j < nm.get(1); ++j) {
table[i][j] = currentLine.toLowerCase().charAt(j);
}
}
int wordsCount = Integer.parseInt(reader.readLine().trim());
List<String> words = new ArrayList<>();
for (int i = 0; i < wordsCount; ++i) {
String currentLine = reader.readLine().toLowerCase();
words.add(currentLine);
}
WheresWaldorf ww = new WheresWaldorf(nm.get(0), nm.get(1), table);
words.forEach(x -> System.out.println(toString(ww.find(x))));
if (k < cases - 1) {
System.out.println();
reader.readLine();
}
}
}
}