-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathboggle.cpp
More file actions
87 lines (72 loc) · 1.67 KB
/
boggle.cpp
File metadata and controls
87 lines (72 loc) · 1.67 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
#include <iostream>
#include <string>
#include <vector>
#include <utility>
using namespace std;
bool search(char (&board)[4][4], string& s, int y, int x, int i,
char (&seen)[4][4]) {
if (i == s.length())
return true;
if (y < 0 || y >= 4 || x < 0 || x >= 4 || seen[y][x] == '1')
return false;
if (board[y][x] != s[i])
return false;
pair<int, int> neighbors[] = {{y - 1, x}, {y + 1, x}, {y, x - 1}, {y, x + 1},
{y - 1, x - 1}, {y - 1, x + 1}, {y + 1, x + 1}, {y + 1, x - 1}};
for (auto& [row, col]: neighbors) {
seen[y][x] = '1';
bool result = search(board, s, row, col, i + 1, seen);
seen[y][x] = '0';
if (result) return true;
}
return false;
}
bool found(char (&board)[4][4], string& s, char (&seen)[4][4]) {
for (int i = 0; i < 4; ++i) {
for (int j = 0;j < 4; ++j) {
if (search(board, s, i, j, 0, seen))
return true;
}
}
return false;
}
int main() {
int n;
cin >> n;
vector<string> words;
words.reserve(n);
string s;
while (n--) {
cin >> s;
words.push_back(s);
}
int scores[] = {0, 0, 0, 1, 1, 2, 3, 5, 11};
char seen[4][4];
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
seen[i][j] = '0';
}
}
cin >> n;
char board[4][4];
while (n--) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
cin >> board[i][j];
}
}
int count = 0, score = 0;
string longestword = "";
for (auto& w: words) {
if (found(board, w, seen)) {
++count;
score += scores[w.length()];
if (w.length() > longestword.length()
|| (w.length() == longestword.length() && w < longestword))
longestword = w;
}
}
cout << score << " " << longestword << " " << count << '\n';
}
return 0;
}