CodeForces - 710C Magic Odd Square(奇数和幻方构造)

Magic Odd Square

Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.

Input

The only line contains odd integer n (1 ≤ n ≤ 49).

Output

Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd.

Examples

Input

1

Output

1

Input

3

Output

2 1 43 5 76 9 8

题意:构造n阶幻方,填入1~n^2,使每行每列每对角线和为奇数。

思路:暴搜TLE,那么考虑构造。方法见下图,1填奇,0填偶。

     0 0 0 0 0 1 0 0 0 0 0     0 0 0 0 1 1 1 0 0 0 0     0 0 0 1 1 1 1 1 0 0 0     0 0 1 1 1 1 1 1 1 0 0     0 1 1 1 1 1 1 1 1 1 0     1 1 1 1 1 1 1 1 1 1 1     0 1 1 1 1 1 1 1 1 1 0     0 0 1 1 1 1 1 1 1 0 0     0 0 0 1 1 1 1 1 0 0 0     0 0 0 0 1 1 1 0 0 0 0     0 0 0 0 0 1 0 0 0 0 0

各类幻方构造法(知乎):https://www.zhihu.com/question/30498489/answer/49208033
#include<bits/stdc++.h>
#define MAX 105
using namespace std;
typedef long long ll;

int a[MAX][MAX],b[MAX*MAX];

int main()
{
    int n,i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n/2;i++){
        j=n/2+1-i+1;
        for(k=1;k<=2*i-1;k++){
            a[i][j+k-1]=1;
        }
    }
    for(i=n/2+1;i<=n;i++){
        j=i-(n/2+1)+1;
        for(k=1;k<=n+1-(2*j-1);k++){
            a[i][j+k-1]=1;
        }
    }
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            if(j>1) printf(" ");
            if(a[i][j]){
                for(k=1;k<=n*n;k+=2){
                    if(b[k]) continue;
                    b[k]=1;
                    a[i][j]=k;
                    break;
                }
            }
            else{
                for(k=2;k<=n*n;k+=2){
                    if(b[k]) continue;
                    b[k]=1;
                    a[i][j]=k;
                    break;
                }
            }
            printf("%d",a[i][j]);
        }
        printf("\n");
    }
    return 0;
 } 
 

原文地址:https://www.cnblogs.com/yzm10/p/10544981.html

时间: 2024-08-01 20:25:36

CodeForces - 710C Magic Odd Square(奇数和幻方构造)的相关文章

codeforces 710C Magic Odd Square(构造或者n阶幻方)

Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd. Input The only line contains odd integer n (1 ≤ n ≤ 49). Output Print n lines with n integers. All the integers should be differ

CodeForces 710C Magic Odd Square (n阶奇幻方)

题意:给它定一个n,让你输出一个n*n的矩阵,使得整个矩阵,每行,每列,对角线和都是奇数. 析:这个题可以用n阶奇幻方来解决,当然也可以不用,如果不懂,请看:http://www.cnblogs.com/dwtfukgv/articles/5797527.html 剩下的就很简单了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <str

【模拟】Codeforces 710C Magic Odd Square

题目链接: http://codeforces.com/problemset/problem/710/C 题目大意: 构造一个N*N的幻方.任意可行解. 幻方就是每一行,每一列,两条对角线的和都相等. 题目思路: [模拟] 分为奇幻方.单偶幻方和双偶幻方三种构造. 具体分类可以查看百度.幻方的N种构造方法 1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algori

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[ {\b

codeforces 710C C. Magic Odd Square(构造)

题目链接: C. Magic Odd Square Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd. Input The only line contains odd integer n (1 ≤ n ≤ 49). Output Print n lines with n integers. All the

Codeforces 710 C. Magic Odd Square(构造)——Educational Codeforces Round 16

传送门 Find an n?×?n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd. Input The only line contains odd integer n(1?≤?n?≤?49). Output Print n lines with n integers. All the integers should be dif

XMU1016 Magic Square【幻方构造】

题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1016 题目大意: 给你一个整数N,然后输出一个N阶幻方. 解题思路: 关于幻方.搜索肯定超时. 可是我们能够直接构造一个幻方. 就是依据奇数阶(2*M+1)幻方. 单偶数阶(4*M+2)幻方.双偶数阶(4*M)幻方等不同的构造方法来构造. 參考博客:http://blog.csdn.net/u012317281/article/details/38051185 1 奇数阶 (2*M+

Java 实现奇数阶幻方的构造

一.设计的流程图如下所示 二.Java 语言的代码实现 package MagicSquare; //奇数幻方的实现 public class Magic_Odd { //n 为幻方的阶数 public static int[][] magicOdd(int n) { //构造一个(n+2)*(n+2)阶的方阵 int[][] square = new int[n + 1][n + 1]; int i = 0; int j = (n + 1) / 2; //从第一行的中间那个数字(是1)开始填幻

求N奇数阶幻方

1. 如果矩阵满足条件,那么对任意,也满足条件.证明显然. 设为奇数,我们现在构造一个n阶幻方包含0到所有数这里x,y满足同余式待确定. 由于该方程组的系数矩阵的行列式为1,所以对任意i,j有唯一解.我们接下来确定a,b: 首先验证每行每列的和均相等,即 由于对任意i,,当x取遍模n的剩余类时,也会取遍所有的剩余类 故每行的和为:同理验证每列. 对于对角线:令b = n - 1, a = (n-1)/2,可以验证y = (n - 1)/2, x = i所以其和为:n*(0 + ... n - 1