E. Road to 1600

$rook$可以走上下左右,$queen$可以上下左右斜。每次找每个可走的最小的走。如果没走完跳跃到某个点继续走花费$1$。

构造$n\times n,rook<queen$。

  • $n<3,-1$
  • $n=3$,暴力$O(9!)$
  • $n>3$构造顺利走完除了$3\times 3$格子。
代码
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 1e3 + 10;
const int mod = 1e9 + 7;
int a[N][N], v[N][N];
void dfs(int x, int y, bool qe)
{
v[x][y] = 1;
int minn = 1e9, ansx, ansy;
for (int i = 1; i <= 3; i++)
if (v[x][i] == 0)
{
if (minn > a[x][i])
{
minn = a[x][i];
ansx = x;
ansy = i;
}
}
for (int i = 1; i <= 3; i++)
if (v[i][y] == 0)
{
if (minn > a[i][y])
{
minn = a[i][y];
ansx = i;
ansy = y;
}
}
if (qe)
{
for (int i = -3; i <= 3; i++)
{
int xx = x + i;
int yy = y + i;
if (xx >= 1 && xx <= 3 && yy >= 1 && yy <= 3 && !v[xx][yy])
{
if (minn > a[xx][yy])
{
minn = a[xx][yy];
ansx = xx;
ansy = yy;
}
}
}
for (int i = -3; i <= 3; i++)
{
int xx = x + i;
int yy = y - i;
if (xx >= 1 && xx <= 3 && yy >= 1 && yy <= 3 && !v[xx][yy])
{
if (minn > a[xx][yy])
{
minn = a[xx][yy];
ansx = xx;
ansy = yy;
}
}
}
}
if (minn == 1e9)
return;
dfs(ansx, ansy, qe);
}
int b[10];
int vis[10];
int ok = 0;
void d(int x)
{
if (ok)
return;
if (x == 10)
{
for (int i = 1; i <= 9; i++)
a[(i - 1) / 3 + 1][(i - 1) % 3 + 1] = b[i];
int ans1 = 0;
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
v[i][j] = 0;
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
if (v[i][j] == 0)
ans1++, dfs(i, j, 0);
int ans2 = 0;
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
v[i][j] = 0;
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
if (v[i][j] == 0)
ans2++, dfs(i, j, 1);
if (ans1 < ans2)
{
ok = 1;
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
cout << a[i][j] << " ";
cout << endl;
}
}

return;
}
for (int i = 2; i <= 9; i++)
{
if (vis[i] == 0)
{
b[x] = i;
vis[i] = 1;
d(x + 1);
vis[i] = 0;
}
}
}
/*
1 2 4
5 3 8
9 6 7
*/
int n;
int main()
{
// vis[1] = 1;
// b[1] = 1;
// d(2);
scanf("%d", &n);
if (n <= 2)
return puts("-1"), 0;
int t = n * n - 9, cur = 0;
a[1][1] = t + 1;
a[1][2] = t + 2;
a[1][3] = t + 4;
a[2][1] = t + 5;
a[2][2] = t + 3;
a[2][3] = t + 8;
a[3][1] = t + 9;
a[3][2] = t + 6;
a[3][3] = t + 7;
for (int i = n; i > 3; i--)
{
if (i & 1)
{
for (int j = 1; j <= i; j++)
a[i][j] = ++cur;
for (int j = i - 1; j >= 1; j--)
a[j][i] = ++cur;
}
else
{
for (int j = 1; j < i; j++)
a[j][i] = ++cur;
for (int j = i; j >= 1; j--)
a[i][j] = ++cur;
}
}
for (int i = 1; i <= n; i++, puts(""))
for (int j = 1; j <= n; j++)
printf("%d ", a[i][j]);
return 0;
}