1
+ #include < iostream>
2
+ #include < algorithm>
3
+ #include < vector>
4
+ using namespace std ;
5
+
6
+ int n;
7
+ vector<vector<int > > map;
8
+ int dx[4 ] = {0 , 0 , 1 , -1 };
9
+ int dy[4 ] = {1 , -1 , 0 , 0 };
10
+
11
+ int changeD[6 ][5 ] = {
12
+ {1 , 0 , 3 , 2 },
13
+ {1 , 3 , 0 , 2 },
14
+ {1 , 2 , 3 , 0 },
15
+ {2 , 0 , 3 , 1 },
16
+ {3 , 0 , 1 , 2 },
17
+ {1 , 0 , 3 , 2 }
18
+ };
19
+
20
+ vector<vector<pair<int , int > > >warmhole;
21
+
22
+ int move (int sx, int sy, int d) {
23
+ int score = 0 ;
24
+ int x = sx, y = sy;
25
+ while (true ) {
26
+ x += dx[d];
27
+ y += dy[d];
28
+ if (x < 0 || x >= n || y < 0 || y >= n) {
29
+ score++;
30
+ d = changeD[0 ][d];
31
+ continue ;
32
+ }
33
+ int point = map[x][y];
34
+ if (point == -1 || (x == sx && y == sy)) {
35
+ break ;
36
+ }
37
+ if (point >= 1 && point <= 5 ) {
38
+ score++;
39
+ d = changeD[point][d];
40
+ }
41
+ else if (point >= 6 && point <= 10 ) {
42
+ for (int i = 0 ; i < 2 ; i++) {
43
+ if (!(warmhole[point-6 ][i].first == x && warmhole[point-6 ][i].second == y)) {
44
+ x = warmhole[point-6 ][i].first ;
45
+ y = warmhole[point-6 ][i].second ;
46
+ break ;
47
+ }
48
+ }
49
+ }
50
+ }
51
+ return score;
52
+
53
+ }
54
+
55
+ int main ()
56
+ {
57
+ ios_base::sync_with_stdio (0 );
58
+ cin.tie (0 );
59
+
60
+ int t, cnt = 1 ;
61
+ cin >> t;
62
+
63
+ while (t--) {
64
+ cin >> n;
65
+ warmhole.assign (5 , vector<pair<int , int > >());
66
+
67
+ map.assign (n, vector<int >(n, 0 ));
68
+ vector<pair<int , int > > emptys;
69
+ for (int i = 0 ; i < n; i++) {
70
+ for (int j = 0 ; j < n; j++) {
71
+ cin >> map[i][j];
72
+ if (map[i][j] == 0 ) emptys.push_back (make_pair (i,j));
73
+ if (map[i][j] >= 6 && map[i][j] <= 10 ) warmhole[map[i][j]-6 ].push_back (make_pair (i,j));
74
+ }
75
+ }
76
+ int result = 0 ;
77
+ for (int i = 0 ; i < emptys.size (); i++) {
78
+ for (int j = 0 ; j < 4 ; j++) {
79
+ result = max (result, move (emptys[i].first , emptys[i].second , j));
80
+ }
81
+ }
82
+ cout << ' #' << cnt << " " << result << ' \n ' ;
83
+ cnt++;
84
+ }
85
+ return 0 ;
86
+ }
0 commit comments