forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboggle.cpp
92 lines (75 loc) · 1.89 KB
/
boggle.cpp
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
#include <bits/stdc++.h>
using namespace std;
vector<vector<bool>> used(4, vector<bool>(4, false));
bool search(vector<vector<char>>& v, string& s, int i, int j, int ch) {
// Base case
if(ch == s.size()) {
return true;
}
// Out of range
if(i < 0 || j < 0 || i == 4 || j == 4) {
return false;
}
// If this spot is already used, quit
if(used[i][j]) {
return false;
}
// If not matching, quit
if(v[i][j] != s[ch]) {
return false;
}
// Search surrounding
for(int x = -1; x <= 1; x++) {
for(int y = -1; y <= 1; y++) {
used[i][j] = true;
bool good = search(v, s, i+x, j+y, ch+1);
used[i][j] = false;
if(good) return true;
}
}
return false;
}
bool found(vector<vector<char>>& v, string& s) {
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(search(v, s, i, j, 0)) return true;
}
}
return false;
}
int main() {
int n;
cin >> n;
vector<string> dict(n);
for(auto& i : dict) {
cin >> i;
}
vector<int> scores = {0,0,0,1,1,2,3,5,11};
int q;
cin >> q;
while(q--) {
vector<vector<char>> v;
v.resize(4, vector<char>(4));
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
cin >> v[i][j];
}
}
int score = 0;
int total = 0;
string word = "";
for(auto& i : dict) {
if(found(v, i)) {
total++;
if(i.length() > word.length()) {
word = i;
}
if(i.length() == word.length() && i < word) {
word = i;
}
score += scores[i.length()];
}
}
cout << score << " " << word << " " << total << endl;
}
}