zoukankan      html  css  js  c++  java
  • HDU 1998 奇数阶魔方【模拟填数/注意边界和细节】

    奇数阶魔方

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 4861    Accepted Submission(s): 2770


    Problem Description
    一个 n 阶方阵的元素是1,2,...,n^2,它的每行,每列和2条对角线上元素的和相等,这样
    的方阵叫魔方。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,从这三个魔方,你可看出“右
    上方”是何意。 
     
    Input
    包含多组数据,首先输入T,表示有T组数据.每组数据1行给出n(3<=n<=19)是奇数。
     
    Output
    对于每组数据,输出n阶魔方,每个数占4格,右对齐
     
    Sample Input
    2
    3
    5
     
    Sample Output
    8 1 6
    3 5 7
    4 9 2
    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
     
    Author
    Zhousc@ECJTU
     
    Source
     
    #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-6;
    const int inf = 0x3f3f3f3f;
    const int N = 100005;
    int n, m, tot;
    int a[50][50];
    int x, y;
    int main()
    {
        int t;
        cin >> t;
        while(t--)
        {
            memset(a, 0, sizeof(a));
            
            cin >> n;
    
            tot = a[x=1][y=(n/2+1)] = 1;
    
            while(tot < n * n) //不等于和等于都可以AC 我TM???
            {
                if( x - 1 <= 0 && y + 1 <= n) //1 最上层
                {
                    a[x=n][++y] = ++tot;   //为什么非要前增 我用后增完全不ojbk
                }
                else if(x - 1 >= 1 && y + 1 <= n && a[x-1][y+1] == 0 ) //normal
                {
                    a[--x][++y] = ++tot;
                }
                else if(x - 1 >= 1 && y + 1 > n)    //4 最右边界
                { 
                    a[--x][y=1] = ++tot;
                }
                else if( (x - 1 <= 0 && y + 1 > n) || ( x - 1 >= 1 && y + 1 <= n && a[x - 1][y + 1] ) )  //28 || 14 最右上角
                {
                    a[++x][y] = ++tot;
                }
    
            }
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=n; j++)
                {
                    printf("%4d",a[i][j]);
                }
                cout<<endl;
            }
    
        }
    
        return 0;
    }
    View Code

    【注释有几个疑问,疑义相与析,谁与我来析呢?】

  • 相关阅读:
    shell-条件测试
    51Nod 1279 扔盘子 (思维+模拟)
    51Nod 1042 数字0-9的数量(数位DP)
    Codeforces 1138B Circus (构造方程+暴力)
    51nod 1133 不重叠的线段 (贪心,序列上的区间问题)
    51nod 1091 线段的重叠(贪心)
    EOJ Monthly 2019.2 E 中位数 (二分+中位数+dag上dp)
    牛客练习赛39 C 流星雨 (概率dp)
    牛客练习赛39 B 选点(dfs序+LIS)
    Educational Codeforces Round 57
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8707830.html
Copyright © 2011-2022 走看看