傳送門
文章目錄
題意:
直接白嫖
思路
首先不難發現,n≤2n\le2n≤2的時候是無解的。
現在我們來構造n=3n=3n=3的情況,通過打表可以發現如下矩陣是符合題目要求的:
179325486\begin{array}{ccc} 1&7&9\\ 3&2&5\\ 4&8&6\\ \end{array}134?728?956?
這就啟發我們有兩種做法:
一是根據這個矩陣發現規律來構造,這個顯然沒什么規律。
二是以這個矩陣為基礎,在其基礎上來加上多于的矩陣,這個方法是比較可行的,讓我們以這個3×33×33×3的矩陣為原型,在其周圍蛇形填數,比如5×55×55×5的矩陣的填法:
1+base7+base9+base783+base2+base5+base694+base8+base6+base5101234111615141312\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+base116?7+base2+base8+base215?9+base5+base6+base314?765413?89101112?
當然在這里base=16base=16base=16,這個顯然是正確的,因為外圍的走完之后其一定停在了row=1row=1row=1或者col=1col=1col=1的位置,之后他們倆都會跑到(1,1)(1,1)(1,1)讓后就掉入圈套辣。
#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
;
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()
{
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;
}
總結
以上是生活随笔為你收集整理的Codeforces Round #632 (Div. 2) E. Road to 1600 构造好题的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。