-
Notifications
You must be signed in to change notification settings - Fork 0
/
swea2501.cpp
80 lines (67 loc) · 1.44 KB
/
swea2501.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
#include <vector>
#include <iostream>
using namespace std;
int n;
int mat[20][20];
bool visit[20][20];
bool desert[101];
int ans;
int dx[4] = { 1, 1, -1, -1 };
int dy[4] = { 1, -1, -1, 1 };
void go(int x, int y, int px, int py, int cnt, int dir) {
if (visit[x][y] && px == x && py == y) {
if (dir == 3 && (ans == -1 || cnt > ans)) {
ans = cnt;
}
return;
}
if (visit[x][y] || desert[mat[x][y]]) {
return;
}
visit[x][y] = desert[mat[x][y]] = true;
if (dir == -1) {
int tx = x + dx[0];
int ty = y + dy[0];
if (!(tx < 0 || ty < 0 || tx > n - 1 || ty > n - 1)) {
go(tx, ty, px, py, cnt + 1, 0);
}
}
else {
int tx = x + dx[dir];
int ty = y + dy[dir];
if (!(tx < 0 || ty < 0 || tx > n - 1 || ty > n - 1)) {
go(tx, ty, px, py, cnt + 1, dir);
}
tx = x + dx[dir + 1];
ty = y + dy[dir + 1];
if (!(tx < 0 || ty < 0 || tx > n - 1 || ty > n - 1)) {
go(tx, ty, px, py, cnt + 1, dir + 1);
}
}
visit[x][y] = desert[mat[x][y]] = false;
return;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tc;
cin >> tc;
for (int k = 1; k <= tc; k++) {
ans = -1;
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> mat[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
memset(visit, 0, sizeof(visit));
memset(desert, 0, sizeof(desert));
go(i, j, i, j, 0, -1);
}
}
cout << "#" << k << " " << ans << '\n';
}
return 0;
}