結果

問題 No.223 1マス指定の魔方陣
ユーザー parukiparuki
提出日時 2016-08-19 22:08:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,889 bytes
コンパイル時間 1,667 ms
コンパイル使用メモリ 170,664 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 08:19:33
合計ジャッジ時間 3,523 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 1 ms
5,376 KB
testcase_46 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

const int LUX[3][2][2] = {{{4,1},{2,3}},{{1,4},{2,3}},{{1,4},{3,2}}};
vector<vector<int> > makeMagicSquare(int n){
    vector<vector<int> > res(n, vector<int>(n));
    if(n % 2 == 1){
        int y = 0, x = n / 2, cur = 0;
        while(cur < n*n){
            while(!res[y][x]){
                res[y][x] = ++cur;
                if(--y < 0)y = n - 1;
                if(++x >= n)x = 0;
            }
            (y += 2) %= n;
            if(--x < 0)x = n - 1;
        }
    } else if(n % 4 == 0){
        int cur = 0;
        for(int y = n - 1; y >= 0; --y)
            for(int x = n - 1; x >= 0; --x)
                res[y][x] = ++cur;
        const int off = n*n + 1;
        for(int sy = 0; sy < n; sy += 4){
            for(int sx = 0; sx < n; sx += 4)
                for(int i = 0; i < 4; ++i)
                    res[sy + i][sx + i] = off - res[sy + i][sx + i];
            for(int sx = 3; sx < n; sx += 4)
                for(int i = 0; i < 4; ++i)
                    res[sy + i][sx - i] = off - res[sy + i][sx - i];
        }
    } else{
        vector<vector<int> > sub = makeMagicSquare(n / 2);
        const int m = n / 2;
        for(int i = 0; i < m; ++i)
            for(int j = 0; j < m; ++j)
                sub[i][j] = 4 * (sub[i][j] - 1);
        auto f = [&](int r, int c, int lux){
            int val = sub[r][c];
            r *= 2;
            c *= 2;
            for(int i = 0; i < 2; ++i)
                for(int j = 0; j < 2; ++j)
                    res[r + i][c + j] = val + LUX[lux][i][j];
        };
        for(int i = 0; i <= m / 2; ++i)
            for(int j = 0; j < m; ++j)
                f(i, j, 0);
        for(int j = 0; j < m; ++j)
            f(m / 2 + 1, j, 1);
        for(int i = m / 2 + 2; i < m; ++i)
            for(int j = 0; j < m; ++j)
                f(i, j, 2);
        f(m / 2, m / 2, 1);
        f(m / 2 + 1, m / 2, 0);
    }
    return res;
}

template<class Val>
void printvv(const vector<vector<Val> > &vv){
    for(size_t i = 0; i < vv.size(); i++)
        for(size_t j = 0; j < vv[i].size(); j++)
            printf("%d%c", vv[i][j], j + 1 != vv[i].size() ? ' ' : '\n');
}

int main(){
    int N, X, Y, Z;
    cin >> N >> X >> Y >> Z;
    auto ans = makeMagicSquare(N);
    --X; --Y; --Z;
    each(v, ans)each(val, v)--val;
    int w = Z^ans[Y][X];
    each(v, ans)each(val, v)val = (val^w) + 1;
    printvv(ans);
}
0