-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
139 lines (112 loc) Β· 3.63 KB
/
Main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package Trie.P9202;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Main {
static int w;
static Trie words = new Trie();
static int b;
static char[][] boogle;
static boolean[][] visited;
static int[] di = {-1, -1, -1, 0, 1, 1, 1, 0};
static int[] dj = {-1, 0, 1, 1, 1, 0, -1, -1};
static int [] score = {0, 0, 0, 1, 1, 2, 3, 5, 11};
static int total, count;
static String max_word;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Trie/P9202/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
w = Integer.parseInt(br.readLine());
for (int i = 0; i < w; i++) {
words.insert(br.readLine());
}
br.readLine();
b = Integer.parseInt(br.readLine());
for (int t = 0; t < b; t++) {
boogle = new char[4][4];
visited = new boolean[4][4];
total = 0;
max_word = "";
count = 0;
for (int i = 0; i < 4; i++) {
String line = br.readLine();
for (int j = 0; j < 4; j++) {
boogle[i][j] = line.charAt(j);
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
char c = boogle[i][j];
if (words.root.hasChild(c))
dfs(i, j, Character.toString(c), words.root.getChild(c));
}
}
System.out.println(total + " " + max_word + " " + count);
words.root.clearHit();
br.readLine();
}
}
static void dfs(int i, int j, String word, Node node) {
visited[i][j] = true;
if (node.isEnd && !node.isHit){
node.isHit = true;
total += score[word.length()];
max_word = compare(max_word, word);
count ++;
}
for (int k = 0; k < 8; k++) {
int find_i = i + di[k];
int find_j = j + dj[k];
if (isValidPath(find_i, find_j)) {
char c = boogle[find_i][find_j];
if (!visited[find_i][find_j] && node.hasChild(c)) {
dfs(find_i, find_j, word + c, node.getChild(c));
}
}
}
visited[i][j] = false;
}
static boolean isValidPath(int i, int j) {
return i >= 0 && i < 4 && j >= 0 && j < 4;
}
static String compare(String old_w, String new_w) {
if (old_w.length() == new_w.length()) {
return old_w.compareTo(new_w) <= 0 ? old_w : new_w;
} else {
return old_w.length() > new_w.length() ? old_w : new_w;
}
}
}
class Trie {
Node root = new Node();
void insert(String word) {
Node currentNode = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (!currentNode.hasChild(c)) {
currentNode.children[c - 'A'] = new Node();
}
currentNode = currentNode.getChild(c);
}
currentNode.isEnd = true;
}
}
class Node {
Node[] children = new Node[26];
boolean isEnd = false;
boolean isHit = false;
Node getChild(char c) {
return children[c - 'A'];
}
boolean hasChild(char c) {
return children[c - 'A'] != null;
}
void clearHit() {
isHit = false;
for (Node child : children) {
if (child != null) {
child.clearHit();
}
}
}
}