首页 > 代码库 > CodeForces 710C Magic Odd Square

CodeForces 710C Magic Odd Square

构造。

先只考虑用$0$和$1$构造矩阵。

$n=1$,$\left[ 1 \right]$。

$n=3$,(在$n=1$的基础上,最外一圈依次标上$0$,$1$,$0$,$1$......)

$\left[ {\begin{array}{*{20}{c}}
0&1&0\\
1&1&1\\
0&1&0
\end{array}} \right]$。

$n=5$,(在$n=3$的基础上,最外一圈依次标上$1$,$0$,$1$,$0$......)

$\left[ {\begin{array}{*{20}{c}}
1&0&1&0&1\\
0&0&1&0&0\\
1&1&1&1&1\\
0&0&1&0&0\\
1&0&1&0&1
\end{array}} \right]$。

输出的时候我们再将$1$至${n^2}$这些数替换$0$和$1$。

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<iostream>using namespace std;typedef long long LL;const double pi=acos(-1.0),eps=1e-8;void File(){    freopen("D:\\in.txt","r",stdin);    freopen("D:\\out.txt","w",stdout);}int a[55][55],n,st;void work(int r1,int c1,int r2,int c2,int f){    for(int j=c1;j<=c2;j++) a[r2][j]=a[r1][j]=f, f=f^1;    f=f^1;for(int i=r1;i<=r2;i++) a[i][c2]=a[i][c1]=f,f=f^1;}int main(){    scanf("%d",&n);    if(n%4==1) st=1; else st=0;    int x1=1,y1=1,x2=n,y2=n;    while(1)    {        work(x1,y1,x2,y2,st);        if(x1==x2&&y1==y2) break;        x1++; y1++; x2--; y2--; st=st^1;    }    int k1=2,k2=1;    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            if(a[i][j]%2==0) a[i][j]=k1, k1=k1+2;            else a[i][j]=k2, k2=k2+2;        }    }    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            printf("%d ",a[i][j]);        }        printf("\n");    }    return 0;}

 

CodeForces 710C Magic Odd Square