-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva00487.cpp
executable file
·68 lines (53 loc) · 1.78 KB
/
uva00487.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
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15);
#define vi vector<int>
using namespace std;
/* ==================== LESSON LEARNED =======================
Don't post recording current answer to its call of next recursion! Because it may won't get into next recursion!
*/
const int dx[] = {0,0,1,-1,1,1,-1,-1};
const int dy[] = {1,-1,0,0,1,-1,1,-1};
struct comp{
bool operator() (const string& lhs, const string& rhs) const{
if(lhs.size() != rhs.size()) return lhs.size() < rhs.size();
else return lhs < rhs;
}
};
int N;
set<string, comp> words;
vector< vector<char> > maze;
vector< vector<bool> > visited;
void findWords(string &str, int x, int y){
str.push_back(maze[x][y]);
visited[x][y] = true;
if(str.size() >= 3) words.insert(str);
int nextX, nextY;
for(int i = 0; i < 8; ++i){
nextX = x+dx[i], nextY = y + dy[i];
if(nextX < 0 || nextX == N || nextY < 0 || nextY == N) continue;
if(!visited[nextX][nextY] && maze[nextX][nextY] > maze[x][y])
findWords(str, nextX, nextY);
}
visited[x][y] = false;
str.pop_back();
}
int main(){ _
int TC; cin >> TC;
for(int count = 0; count < TC; ++count){
if(count) cout << endl;
cin >> N;
maze.assign(N, vector<char> (N));
visited.assign(N, vector<bool> (N, false));
words.clear();
for(int i = 0; i < N; ++i)
for(int j = 0; j < N; ++j)
cin >> maze[i][j];
string str;
for(int i = 0; i < N; ++i)
for(int j = 0; j < N; ++j)
findWords(str, i, j);
for(set<string>::iterator it = words.begin(); it != words.end(); ++it)
cout << *it << endl;
}
return 0;
}