-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path487-Boggle Blitz.cpp
46 lines (43 loc) · 1.11 KB
/
487-Boggle Blitz.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
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> dirs = {{0,1},{1,0},{-1,0},{0,-1},{-1,-1},{1,1},{-1,1},{1,-1}};
auto cmp = [](const string& a,const string& b){
if(a.length() == b.length()) return a<b;
else return a.length() < b.length();
};
set<string,decltype(cmp)> res(cmp);
void dfs(int r,int c,string& cur,vector<string> grid){
if(r<0||c<0||r>=grid.size()||c>=grid.size()) return;
if(!cur.empty() && grid[r][c] <= cur.back()) return;
cur += grid[r][c];
if(cur.length() > 2) res.insert(cur);
for(auto& dir : dirs){
int x = r+dir[0];
int y = c+dir[1];
dfs(x,y,cur,grid);
}
cur.pop_back();
}
int main()
{
int t,n;
string in;
cin >> t;
while(t--){
vector<string> grid;
cin >> n;
for(int i=0;i<n;i++){
cin >> in;
grid.push_back(in);
}
res.clear();
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
string cur = "";
dfs(i,j,cur,grid);
}
for(auto& word : res)
cout << word << endl;
if(t) cout << endl;
}
}