結果

問題 No.217 魔方陣を作ろう
ユーザー firiexpfiriexp
提出日時 2019-11-21 10:25:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,688 bytes
コンパイル時間 875 ms
コンパイル使用メモリ 99,784 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-09 21:18:34
合計ジャッジ時間 2,694 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,388 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <limits>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> v(n, vector<int>(n, 0));
    if(n%2){
        int y = n-1, x = n/2;
        int cnt = 1;
        while(cnt <= n*n){
            v[y][x] = cnt++;
            int yy = (y+1)%n, xx = (x+1)%n;
            if(v[yy][xx]) yy = (y+n-1)%n, xx = x;
            y = yy, x = xx;
        }
    }else if(n%4 == 0){
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if((((i+1)%4 >= 2) ^ (((j+1)%4) >= 2))){
                    v[n-i-1][n-j-1] = i*n+j+1;
                }else {
                    v[i][j] = i*n+j+1;
                }
            }
        }
    }else {
        int m = n/2;
        vector<vector<int>> u(m, vector<int>(m, -1));
        int y = m-1, x = m/2;
        int cnt = 1;
        while(cnt <= m*m){
            u[y][x] = cnt++;
            int yy = (y+1)%m, xx = (x+1)%m;
            if(u[yy][xx] != -1) yy = (y+m-1)%m, xx = x;
            y = yy, x = xx;
        }
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < m; ++j) {
                u[i][j]--;
                if(m/2+1 < i){
                    v[2*i][2*j] = u[i][j]*4+1;
                    v[2*i+1][2*j] = u[i][j]*4+3;
                    v[2*i][2*j+1] = u[i][j]*4+4;
                    v[2*i+1][2*j+1] = u[i][j]*4+2;
                }else if(m/2+1 == i && j != m/2){
                    v[2*i][2*j] = u[i][j]*4+1;
                    v[2*i+1][2*j] = u[i][j]*4+2;
                    v[2*i][2*j+1] = u[i][j]*4+4;
                    v[2*i+1][2*j+1] = u[i][j]*4+3;
                }else if(i == m/2 && j == m/2){
                    v[2*i][2*j] = u[i][j]*4+1;
                    v[2*i+1][2*j] = u[i][j]*4+2;
                    v[2*i][2*j+1] = u[i][j]*4+4;
                    v[2*i+1][2*j+1] = u[i][j]*4+3;
                }else {
                    v[2*i][2*j] = u[i][j]*4+4;
                    v[2*i+1][2*j] = u[i][j]*4+2;
                    v[2*i][2*j+1] = u[i][j]*4+1;
                    v[2*i+1][2*j+1] = u[i][j]*4+3;
                }
            }
            puts("");
        }
    }
    
    for (int i = 0; i < n; ++i) {
        int s = 0;
        for (int j = 0; j < n; ++j) {
            if(j) printf(" ");
            printf("%d", v[i][j]);
        }
        puts("");
    }
    return 0;
}
0