time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

We will consider the numbers aa and bb as adjacent if they differ by exactly one, that is, |a−b|=1|a−b|=1.

We will consider cells of a square matrix n×nn×n as adjacent if they have a common side, that is, for cell (r,c)(r,c) cells (r,c−1)(r,c−1), (r,c+1)(r,c+1), (r−1,c)(r−1,c) and (r+1,c)(r+1,c) are adjacent to it.

For a given number nn, construct a square matrix n×nn×n such that:

  • Each integer from 11 to n2n2 occurs in this matrix exactly once;
  • If (r1,c1)(r1,c1) and (r2,c2)(r2,c2) are adjacent cells, then the numbers written in them must not be adjacent.

Input

The first line contains one integer tt (1≤t≤1001≤t≤100). Then tt test cases follow.

Each test case is characterized by one integer nn (1≤n≤1001≤n≤100).

Output

For each test case, output:

  • -1, if the required matrix does not exist;
  • the required matrix, otherwise (any such matrix if many of them exist).

The matrix should be outputted as nn lines, where each line contains nn integers.

Example

input

Copy

3
1
2
3

output

Copy

1
-1
2 9 7
4 6 3
1 8 5

解题说明:此题是一道模拟题,找规律用构造法来做。

#include<stdio.h>
#include<math.h>
int main()
{long long int a[200][200], n, i, j, p, t, k;scanf("%lld", &t);while(t--){scanf("%lld", &n);if(n==2){printf("-1\n");}else{p=1;k=n+n*(n-1)/2;for(j=0; j<n-1; j++){for(i=0; i<(n-j); i++){a[i][i+j]=p;p++;if(j>0){a[i+j][i]=k;k++;}}}a[0][n-1]=n*n-1;a[n-1][0]=n*n;for(i=0; i<n; i++){for(j=0; j<n; j++){printf("%lld ", a[i][j]);}printf("\n");}}}return 0;
}

C. Not Adjacent Matrix相关推荐

  1. adjacent matrix和affinity matrix之间的区别

    本文描述adjacent matrix和affinity matrix 的异同点. 如下图是一个无向图,其有4个点,5条边,每个边的权重为1. 如果使用 adjacent matrix描述该图,则ad ...

  2. 数据科学中必须知道的5个关于奇异值分解(SVD)的应用

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文转自|机器学习算法那些事 前言:本文为大家介绍了5个关于奇异值 ...

  3. iMeta | 中科院生态中心邓晔组发布微生物组网络分析平台iNAP(图文/视频教程)

    点击蓝字 关注我们 针对微生物组学研究的集成网络分析平台 https://doi.org/10.1002/imt2.13 2022/3/16 ● 2022年3月16日,中国科学院生态环境研究中心邓晔组 ...

  4. Visual Transformers: Token-based Image Representation and Processing for Computer Vision

    作者:今天不吹牛 链接:https://www.zhihu.com/question/400733777/answer/1466879756 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权, ...

  5. 《大话数据结构》读书笔记-图

    写在前面:本文仅供个人学习使用.<大话数据结构>通俗易懂,适合整体做笔记输出,构建体系.并且文中很多图片来源于该书,如有侵权,请联系删除. 文章目录 7.2 图的定义 7.2.1 各种图定 ...

  6. 武大上交发布首篇「图像匹配」大领域综述!涵盖 8 个子领域,汇总近 20年经典方法

    原文链接:https://bbs.cvmart.net/topics/3176 专注计算机视觉前沿资讯和技术干货 微信公众号:极市平台 官网:https://www.cvmart.net/ 武汉大学和 ...

  7. 无向图:查找最小环集合(最短路径回溯算法)

    在无向图中查找最小环,就像需要查找一个蜂窝中所有孔洞,如果只查找数目,可以利用欧拉公式,若查找到所有环,需要更进一步的搜索. 方法:寻找到所有顶点的最短路径,对每一个顶点,取出环,循环删除顶点,输出所 ...

  8. 【论文阅读】A Gentle Introduction to Graph Neural Networks [图神经网络入门](7)

    [论文阅读]A Gentle Introduction to Graph Neural Networks [图神经网络入门](7) Into the Weeds Other types of grap ...

  9. 文本摘要提取_了解自动文本摘要-1:提取方法

    文本摘要提取 Text summarization is commonly used by several websites and applications to create news feed ...

  10. Codeforces Round #719 (Div. 3)/ Codeforces Round #720 (Div. 2)

    A. Do Not Be Distracted! 题意: 一件事情一但开始,只能做完才能做别的事,当出现一件事不连续出现时,教师会怀疑 题目: Polycarp has 26 tasks. Each ...

最新文章

  1. 基于深度学习识别模型的缺陷检测
  2. Swoole同步/异步/阻塞/非阻塞的理解
  3. Python爬虫快速入门,BeautifulSoup基本使用及实践
  4. 视图计算背后的技术架构思考
  5. BOA+CGI+SQLite 随笔
  6. rocketmq 消息 自定义_跟我学RocketMQ[1-4]之消息消费及支持spring
  7. Soul网关源码阅读(六)请求类型探索
  8. TensorFlow-谷歌深度学习库 数据读取器
  9. Atitit 减少财政支出之减少通讯支出 解决方案attilax总结
  10. Android 导入导出excel xls、xlsx
  11. 高斯函数与高斯核函数
  12. 网络工程师--网络规划和设计案例分析(1)
  13. Automatic Lip-reading with Hierarchical Pyramidal Convolution and Self-Attention for Image Sequences
  14. 适用于 Windows 操作系统的远程服务器管理工具 (RSAT)
  15. 软件项目管理三国启示录01 群雄争霸之项目经理的自我修养
  16. 七、vertical-align属性、透明度属性及兼容、ps常用工具、常见的图片格式、项目规范、命名参考、iconfont的使用
  17. Android 解析软件包时出现问题 -- Error staging apk from content URI
  18. 什么是车联网,IoV(Internet of Vehicles)
  19. 对面向过程,对象,接口,切面的理解
  20. php判断caj文件页数,2个免费CAJ转PDF的方法,而且不限页数和大小

热门文章

  1. echart柱状图横向_echarts 横向柱状图TOP10
  2. 静水流深,闻喧享静 空山鸣响,见惯司空
  3. 【短信验证】短信验证 短信API使用方法含code【亲测可用】【实用】【超详细】
  4. 如何给客户做产品培训
  5. Python Computer Vision Programming学习笔记(二)——基本的图像操作与处理
  6. Spring Boot2 系列教程(三十三)整合 Spring Security
  7. 基于MySQL的京东用户行为分析
  8. 谷粒商城P46 gulimall-gateway刷新验证码出现503错误
  9. nacos + springcloud gateway 出现503错误
  10. 周日报名截止,翼支付杯大数据建模大赛16万大奖邀你来!!