Codeforces 710C_Magic Odd Square(构造)
来源:互联网 发布:拔罐 菲尔普斯 知乎 编辑:程序博客网 时间:2024/05/29 18:10
C. Magic Odd Square
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputFind 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
幻方的思想求解,用dfs搜索:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int ch[50][50];int n;void dfs(int x,int y,int v){ int x1,y1; ch[x][y]=v; if(x-1<1) x1=n; else x1=x-1; if(y+1>n) y1=1; else y1=y+1; if(ch[x1][y1]==0) { dfs(x1,y1,v+1); } else { if(x+1>n) x1=1; else x1=x+1; if(ch[x1][y]==0) { dfs(x1,y,v+1); } else return ; }}int main(){ cin>>n; memset(ch,0,sizeof(ch)); dfs(1,n/2+1,1); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { cout<<ch[i][j]<<" "; } cout<<endl; } return 0;}
0 0
- Codeforces 710C_Magic Odd Square(构造)
- codeforces 710 C. Magic Odd Square(构造)
- CodeForces-710C Magic Odd Square(幻方问题/构造)
- CodeForces 710 Magic Odd Square
- Codeforces 710 C. Magic Odd Square(构造)——Educational Codeforces Round 16
- codeforces 710C Magic Odd Square
- 【模拟】Codeforces 710C Magic Odd Square
- Codeforces 710C-Magic Odd Square
- Codeforces Problem 710C Magic Odd Square
- codeforces 710C Magic Odd Square 【杂类】
- codeforces 710C Magic Odd Square
- CodeForces 710C Magic Odd Square(数学)
- 【codeforces 710 C Magic Odd Square】
- codeforces 710C C. Magic Odd Square
- Codeforces 710C Magic Odd Square【思维】
- Educational Codeforces Round 16 C. Magic Odd Square (构造)
- 【Educational Codeforces Round 16】Codeforces 710C Magic Odd Square
- codeforces 710C Magic Odd Square(规律)
- 直播架构
- java中字符串的增加,插入及StringBuffer的使用
- 小米 muiu
- Unity DoTween 简明快速使用教程
- HTML5
- Codeforces 710C_Magic Odd Square(构造)
- 玩无限消乐的设计示例(高级篇)积分与特效的数值加载
- Visual Studio 2012 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案
- 路由器的工作原理
- typedef struct和struct区别
- LOG4J2 异步日志
- POJ 1523 Tarjan求割点
- PHP底层赋值原理
- Android项目集成信鸽推送过程详解