【模拟】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<algorithm> 6 #include<string> 7 #include<iomanip> 8 #include<map> 9 #include<memory.h> 10 #include<time.h> 11 #include<stdio.h> 12 #include<stdlib.h> 13 #include<string.h> 14 //#include<stdbool.h> 15 #include<math.h> 16 #define min(a,b) ((a)<(b)?(a):(b)) 17 #define max(a,b) ((a)>(b)?(a):(b)) 18 #define abs(a) ((a)>0?(a):(-(a))) 19 #define lowbit(a) (a&(-a)) 20 #define sqr(a) ((a)*(a)) 21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) 22 #define mem(a,b) memset(a,b,sizeof(a)) 23 #define eps (1e-8) 24 #define J 10 25 #define mod 1000000007 26 #define MAX 0x7f7f7f7f 27 #define PI 3.14159265358979323 28 #define N 54 29 using namespace std; 30 typedef long long LL; 31 int cas,cass; 32 int n,m,lll,ans; 33 int a[N][N]; 34 void work1(int xx,int yy,int nn,int s,int t) 35 { 36 int i,x,y; 37 x=xx+0;y=yy+nn/2; 38 a[x][y]=s; 39 for(i=s+1;i<=t;i++) 40 { 41 x=(x-1+nn)%nn+xx; 42 y=(y+1)%nn+yy; 43 if(a[x][y])x=(x+2)%nn+xx,y=(y-1+nn)%nn+yy; 44 a[x][y]=i; 45 } 46 } 47 void work2() 48 { 49 int i,j,k; 50 m=n/2;k=m/2; 51 work1(0,0,m,1,sqr(m)); 52 work1(m,m,m,sqr(m)+1,sqr(m)*2); 53 work1(0,m,m,sqr(m)*2+1,sqr(m)*3); 54 work1(m,0,m,sqr(m)*3+1,sqr(m)*4); 55 for(i=0,j=m+m/2;i<m;i++) 56 swap(a[i][j],a[i+m][j]); 57 for(i=0;i<m;i++) 58 { 59 if(i==k)continue; 60 for(j=0;j<k;j++) 61 swap(a[i][j],a[i+m][j]); 62 } 63 for(i=0;i<k;i++) 64 swap(a[k][i+k],a[k+m][i+k]); 65 } 66 void work3() 67 { 68 int i,j,k; 69 m=n*n+1; 70 for(i=0;i<n;i++) 71 for(j=0;j<n;j++) 72 a[i][j]=i*n+j+1; 73 for(i=0;i<n;i+=4) 74 { 75 for(j=0;j<n;j+=4) 76 { 77 for(k=0;k<4;k++) 78 { 79 a[i+k][j+k]=m-a[i+k][j+k]; 80 a[i+k][j+3-k]=m-a[i+k][j+3-k]; 81 } 82 } 83 } 84 } 85 int main() 86 { 87 #ifndef ONLINE_JUDGE 88 // freopen("1.txt","r",stdin); 89 // freopen("2.txt","w",stdout); 90 #endif 91 int i,j,k; 92 // for(scanf("%d",&cas);cas;cas--) 93 // for(scanf("%d",&cas),cass=1;cass<=cas;cass++) 94 // while(~scanf("%s",s+1)) 95 while(~scanf("%d",&n)) 96 { 97 if(n&1) 98 work1(0,0,n,1,sqr(n)); 99 else if(n%4==0) 100 work3(); 101 else 102 work2(); 103 for(i=0;i<n;i++) 104 { 105 for(j=0;j<n;j++) 106 printf("%d ",a[i][j]); 107 puts(""); 108 } 109 } 110 return 0; 111 } 112 /* 113 // 114 115 // 116 */
View Code
转载于:https://www.cnblogs.com/Coolxxx/p/5797791.html
【模拟】Codeforces 710C Magic Odd Square相关推荐
- Educational Codeforces Round 16 C. Magic Odd Square 矩阵构造
传送门 文章目录 题意: 思路: 题意: 给你一个奇数nnn,让你构造一个n∗nn*nn∗n的矩阵,矩阵的每个位置依次填上[1,n∗n]之内的数[1,n*n]之内的数[1,n∗n]之内的数,满足每行. ...
- Magic Odd Square 思维
Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both ma ...
- 689D Magic Odd Square 奇数幻方
1 奇数阶幻方构造法 (1) 将1放在第一行中间一列; (2) 从2开始直到n×n止各数依次按下列规则存放:按 45°方向行走,向右上,即每一个数存放的行比前一个数的行数减1,列数加1 (3) 如果行 ...
- 模拟 Codeforces Round #249 (Div. 2) C. Cardiogram
题目地址:http://codeforces.com/contest/435/problem/C 1 /* 2 题意:给一组公式,一组数据,计算得到一系列的坐标点,画出折线图:) 3 模拟题:蛮恶心的 ...
- 模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie
题目传送门 1 /* 2 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 3 题目倒是很长:) 4 */ 5 #include <cstdio> 6 #include <al ...
- 【CodeForces - 1A】Theatre Square(水题,几何)(CODEFORCES,梦的开始)
题干: Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters ...
- codeforces 670D1 Magic Powder - 1
题目链接:http://codeforces.com/problemset/problem/670/D1 题目:Magic Powder - 1 time limit per test 1 secon ...
- Codeforces 424C Magic Formulas
题目链接:[这里写链接内容] (http://codeforces.com/contest/424/problem/C) 题面: C. Magic Formulas(神奇的矩阵) time limit ...
- CodeForces - 981G Magic multisets
假设我们可以对每个位置快速维护一个数组,记录每个位置有哪些值是已经出现了的,哪些值是没有出现的,这样就可以决定修改的时候到底是 *2 还是 +1了. 但是很可惜,并不存在功能这么强大的数组,所以只能另 ...
最新文章
- Selenium 获取文本信息方法+select(定位)
- Qt之QSpacerItem
- Java基础知识复习(二)
- ANTLR入门:构建一种简单的表达语言
- Linux内存管理之基本概念介绍(一)
- 【Python】 Python数据类型
- iOS从生成证书到打包上架-02(详细2016-10最新)
- 计算机系元旦主题,元旦主题活动方案
- mysql 乱码处理
- springcloud配置负载均衡 及方式_Springcloud-Ribbon负载均衡NODO
- jszip 解压压缩包_一文彻底弄懂jszip中的压缩与解压
- python MDI窗口加载ui文件方法
- WIN7各种系统大全
- Google Docs 简介
- python怎么定义未知数_码如其人,同学你能写一手漂亮的Python函数吗
- ​Au入门系列之二:波形编辑
- 任务分配到每个工作日
- Leetcode之恰有K根木棍可以看到的排列数目
- 宅急送 项目第四天 取派员和区域管理
- Windows用命令压缩和解压