HDU 1998 奇数阶魔方【模拟填数/注意边界和细节】
奇数阶魔方
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4861 Accepted Submission(s): 2770
的方阵叫魔方。n为奇数时我们有1种构造方法,叫做“右上方” ,例如下面给出n=3,5,7时
的魔方.
3
8 1 6
3 5 7
4 9 2
5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
第1行中间的数总是1,最后1行中间的数是n^2,他的右边是2,从这三个魔方,你可看出“右
上方”是何意。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<set>
#include<map>
#include<sstream>
#include<queue>
#include<cmath>
#include<list>
#include<vector>
#include<string> using namespace std; #define long long ll
const double PI = acos(-1.0);
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int N = ;
int n, m, tot;
int a[][];
int x, y;
int main()
{
int t;
cin >> t;
while(t--)
{
memset(a, , sizeof(a)); cin >> n; tot = a[x=][y=(n/+)] = ; while(tot < n * n) //不等于和等于都可以AC 我TM???
{
if( x - <= && y + <= n) //1 最上层
{
a[x=n][++y] = ++tot; //为什么非要前增 我用后增完全不ojbk
}
else if(x - >= && y + <= n && a[x-][y+] == ) //normal
{
a[--x][++y] = ++tot;
}
else if(x - >= && y + > n) //4 最右边界
{
a[--x][y=] = ++tot;
}
else if( (x - <= && y + > n) || ( x - >= && y + <= n && a[x - ][y + ] ) ) //28 || 14 最右上角
{
a[++x][y] = ++tot;
} }
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
printf("%4d",a[i][j]);
}
cout<<endl;
} } return ;
}
【注释有几个疑问,疑义相与析,谁与我来析呢?】
HDU 1998 奇数阶魔方【模拟填数/注意边界和细节】的更多相关文章
- hdu 1998 奇数阶魔方(找规律+模拟)
应该不算太水吧. 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 ...
- 杭电ACM 1998奇数阶魔方
#include<stdio.h>#include <string.h>int main(){ int n,m; int a[40][40]={0}; scanf(" ...
- 算法:九宫格问题--奇数阶魔方(Magic-Square)
一.魔方介绍 魔方(这里是简称,也可以叫幻方.魔术矩阵,Magic Square)是 n×n 正方形网格(n 为每侧的单元数),里面每个单元格填充了不同的正整数 1, 2, 3, ... , n2,并 ...
- hdoj 2183 奇数阶魔方(II) 【模拟】+【法】
比赛的时候花了一个多小时,以做不做 分析:可观察:中间是(n*n+1)/2, 中间的上面是n*n,以下是1, 左边是n,右面是(n*n+1)-n,并且正对角线是最左上对到最右下端添加(+1).另外一条 ...
- 任意阶魔方阵(幻方)的算法及C语言实现
写于2012.10: 本来这是谭浩强那本<C程序设计(第四版)>的一道课后习题,刚开始做得时候去网上找最优的算法,结果发现奇数和双偶数(4的倍数)的情况下算法都比较简单,但是单偶数(2的倍 ...
- noip模拟12[简单的区间·简单的玄学·简单的填数]
noip模拟12 solutions 这次考试靠的还是比较好的,但是还是有不好的地方, 为啥嘞??因为我觉得我排列组合好像白学了诶,文化课都忘记了 正难则反!!!!!!!! 害没关系啦,一共拿到了\( ...
- n阶魔方阵(奇数阵)的输出
需求 要求输出1~n²的自然数构成的魔方阵. STEP 1 什么是魔方阵? 魔方阵,古代又称“纵横图”,是指组成元素为自然数1.2…n2的平方的n×n的方阵,其中每个元素值都不相等,且每行.每列以及主 ...
- nyoj 33-蛇形填数 (循环,模拟)
33-蛇形填数 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:15 submit:38 题目描述: 在n*n方陈里填入1,2,...,n*n,要求填 ...
- [CSP-S模拟测试]:简单的填数(贪心+模拟)
题目描述 对于一个长度为$n$,且下标从$1$开始编号的序列$a$,我们定义它是「合法的」,当且仅当它满足以下条件:·$a_1=1$·对于$i\in [1,n),a_i\leqslant a_{i+1 ...
随机推荐
- [Poj2761]Feed the dogs(主席树)
Desciption 题意:求区间第K小(N<=100000) Solution 主席树模板题 Code #include <cstdio> #include <algorit ...
- Install ADDS on Windows Server 2012 R2 with PowerShell
Install ADDS on Windows Server 2012 R2 with PowerShell Posted by ethernuno on 20/04/2014 In this tut ...
- Google Chrome开发者工具-移动仿真:触摸事件仿真
如果你在开发PAD/手机所用WEB版应用,需要在桌面审查页面元素.调试脚本,模拟移动设备尺寸.事件.位置等信息, 那么可以使用Chrome开发者工具(DevTools)提供的强大的移动仿真功能,支持主 ...
- PostgreSQL drop database 显示会话没有关闭 [已解决]
错误重现 有时候需要删除某个数据库时,会报如下错误,显示有一个连接正在使用数据库,无法删除 ERROR: database "pilot" is being accessed by ...
- [oldboy-django][6其他]微信二维码扫码登录注册
http://blog.csdn.net/maerdym/article/details/46380309 http://blog.csdn.net/jiang1013nan/article/deta ...
- jquery版右下角弹窗效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Puppet单机实战之Nginx代理Tomcat
author:JevonWei 版权声明:原创作品 blog:http://119.23.52.191/ --- 构建实战之Nginx代理Tomcat [root@node1 modules]# mk ...
- Pointcut is not well-formed: expecting 'identifier' at character position 0 ^ || Pointcut is not well-formed: expecting ')' at character position 11 ^
错误提示: 解决方法1:指定execution 在执行目标方法之前指定execution 解决方法2:可能是execution写错了.请仔细检查. 其他——execution参数设置(带问好的可以不配 ...
- git pull命令模式
git pull 默认等于 git fetch + git mergegit pull --rebase 等于 git fetch + git rebasegit pull -–squash 等于 g ...
- Codeforces Round #364 (Div. 2) C 二分处理+求区间不同字符的个数 尺取法
C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...