forked from oeddyo/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path487.cpp
More file actions
104 lines (96 loc) · 2.32 KB
/
487.cpp
File metadata and controls
104 lines (96 loc) · 2.32 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
using namespace std;
int N;
char data[21][21];
bool used[21][21];
int dir[21][21][8];
int dir_cnt[21][21];
typedef pair<int, int> pii;
typedef pair<int, pii > piii;
vector< pair<int, string> > ans;
int my_d[8][2] = {{0,1},{0,-1},{1,0},{-1,0},{-1,-1},{-1,1},{1,-1},{1,1}};
bool check(int i, int j){
if(i>=0 && i<N && j>=0 && j<N){
return true;
}
return false;
}
void search_direction(){
for(int i=0; i<N; i++){
for(int j=0; j<N;j++){
int cnt = 0;
vector< pii > t_v;
for(int k=0; k<8; k++){
int nx = i+my_d[k][0];
int ny = j+my_d[k][1];
if(check(nx, ny)&&data[i][j]<data[nx][ny]){
cnt++;
//cout<<"at "<<i<<" "<<j<<" could go to "<<nx<<" "<<ny<<endl;
t_v.push_back( pii(data[nx][ny]-'a', k));
}
}
sort(t_v.begin(), t_v.begin()+cnt);
dir_cnt[i][j] = cnt;
for(int k=0; k<cnt; k++){
dir[i][j][k] = t_v[k].second;
}
}
}
}
void dfs(int x, int y, int len, string s){
if(len>=3){
ans.push_back(pair<int, string>(len, s));
}
for(int i=0; i<dir_cnt[x][y]; i++){
int nx = x+my_d[dir[x][y][i]][0];
int ny = y+my_d[dir[x][y][i]][1];
//cout<<nx<<" "<<ny<<endl;
if(used[nx][ny]==false){
used[nx][ny]=true;
dfs(nx,ny,len+1, s+data[nx][ny]);
used[nx][ny]=false;
}
}
}
void work(){
ans.clear();
search_direction();
memset(used,0,sizeof(used));
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
string tt = "";
tt+=data[i][j];
dfs(i,j,1, tt);
}
}
sort(ans.begin(), ans.end());
int sz = ans.size();
string pre = "";
for(int i=0; i<sz; i++){
if(ans[i].second!=pre){
pre = ans[i].second;
cout<<ans[i].second<<endl;
}
}
}
int main(){
int T;
cin>>T;
int blank = 0;
while(T--){
if(blank>=1){
cout<<endl;
}
blank++;
cin>>N;
for(int i=0; i<N; i++){
cin>>data[i];
}
work();
}
return 0;
}