Codeforces Round #719 (Div. 3) C. Not Adjacent Matrix

地址 Problem - C - Codeforces

 

题意

每个格子,该格子和相邻的格子的值不能相同

题解

思维题, 先从1~n输出奇数,再输出偶数

代码

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

typedef long long LL;

int a[110][110];

int main()
{
    LL n, t;
    cin >> t;
    while(t --)
    {
        cin >> n;
        if(n == 2)
        {
            puts("-1");
            continue;
        }
        int step = -1;
        for(int i = 1; i <= n; i ++)
        {
            for(int j = 1; j <= n; j ++)
            {
                step += 2;
                if(step > n*n)    step = 2;
                cout << step << ' ';
            }     
            puts("");
        }
    }
    return 0;
}

 

posted @ 2021-05-07 17:28  la-la-wanf  阅读(33)  评论(0编辑  收藏  举报