题意:
直接白嫖
思路
首先不难发现,
n
≤
2
n\le2
n≤2的时候是无解的。
现在我们来构造
n
=
3
n=3
n=3的情况,通过打表可以发现如下矩阵是符合题目要求的:
1
7
9
3
2
5
4
8
6
\begin{array}{ccc} 1&7&9\\ 3&2&5\\ 4&8&6\\ \end{array}
134728956
这就启发我们有两种做法:
一是根据这个矩阵发现规律来构造,这个显然没什么规律。
二是以这个矩阵为基础,在其基础上来加上多于的矩阵,这个方法是比较可行的,让我们以这个
3
×
3
3×3
3×3的矩阵为原型,在其周围蛇形填数,比如
5
×
5
5×5
5×5的矩阵的填法:
1
+
b
a
s
e
7
+
b
a
s
e
9
+
b
a
s
e
7
8
3
+
b
a
s
e
2
+
b
a
s
e
5
+
b
a
s
e
6
9
4
+
b
a
s
e
8
+
b
a
s
e
6
+
b
a
s
e
5
10
1
2
3
4
11
16
15
14
13
12
\begin{array}{ccc} 1+base&7+base&9+base&7&8\\ 3+base&2+base&5+base&6&9\\ 4+base&8+base&6+base&5&10\\ 1&2&3&4&11\\ 16&15&14&13&12 \end{array}
1+base3+base4+base1167+base2+base8+base2159+base5+base6+base31476541389101112
当然在这里
b
a
s
e
=
16
base=16
base=16,这个显然是正确的,因为外围的走完之后其一定停在了
r
o
w
=
1
row=1
row=1或者
c
o
l
=
1
col=1
col=1的位置,之后他们俩都会跑到
(
1
,
1
)
(1,1)
(1,1)让后就掉入圈套辣。
// Problem: E. Road to 1600
// Contest: Codeforces - Codeforces Round #632 (Div. 2)
// URL: https://codeforces.com/contest/1333/problem/E
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=510,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n;
int a[N][N];
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
cin>>n;
if(n<=2) {
puts("-1");
return 0;
}
int now=0;
for(int i=4;i<=n;i++) {
if(i%2==0) {
for(int j=1;j<=i;j++) a[i][j]=++now;
for(int j=i-1;j>=1;j--) a[j][i]=++now;
}
else {
for(int j=1;j<=i;j++) a[j][i]=++now;
for(int j=i-1;j>=1;j--) a[i][j]=++now;
}
}
a[1][1]=now+1; a[1][2]=now+7; a[1][3]=now+9;
a[2][1]=now+3; a[2][2]=now+2; a[2][3]=now+5;
a[3][1]=now+4; a[3][2]=now+8; a[3][3]=now+6;
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) printf("%d ",a[i][j]);
puts("");
}
return 0;
}
/*
*/