結果

問題 No.217 魔方陣を作ろう
ユーザー cormorancormoran
提出日時 2016-12-06 19:03:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,896 bytes
コンパイル時間 1,919 ms
コンパイル使用メモリ 181,164 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 19:37:33
合計ジャッジ時間 3,062 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using pii = pair<int,int>;
using ll = long long;
#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define rrep(i, j) for(int i=(int)(j-1); i >=0; i--)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)
#define all(v) v.begin(),v.end()
#define debug(x) cerr << #x << " : " << x << endl

template<class T> bool set_min(T &a, const T &b) { return a > b  ? a = b, true : false; }
template<class T> bool set_max(T &a, const T &b) { return a < b  ? a = b, true : false; }
// vector
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
template<class T> ostream& operator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << "\t" << t; return os << endl; }
// pair
template<class T, class U> ostream& operator << (ostream &os , const pair<T, U> &v) { return os << "<" << v.first << ", " << v.second << ">"; }

const int INF = 1 << 30;
const ll INFL = 1LL << 60;


class Solver {
  public:

    vector<vector<int>> odd_magic_square(int N) {
        assert(N % 2);
        vector<vector<int>> G(N, vector<int>(N));
        int nx = N / 2;
        int ny = 0;
        rep(i, N * N) {            
            G[ny][nx] = i + 1;
            while(i != N * N - 1 and G[ny][nx]) {
                int nny = (ny + N - 1) % N;
                int nnx = (nx + 1) % N;
                if(G[nny][nnx]) ny = (ny + 1) % N;
                else nx = nnx, ny = nny;
            }
        }
        return G;
    }
    vector<vector<int>> four_times_magic_square(int N) {
        assert(N % 4 == 0);
        vector<vector<int>> G(N, vector<int>(N));
        vector<bool> used(N * N);
        rep(y, N) rep(x, N) {
            if(x % 4 == y % 4 or x % 4 == 3 - y % 4) {
                int n = y * N + x + 1;
                G[y][x] = n;
                used[n] = true;
            }
        }
        int n = 1;
        rrep(y, N) rrep(x, N) {
            if(not(x % 4 == y % 4 or x % 4 == 3 - y % 4)) {
                while(used[n]) n++;
                G[y][x] = n;
                used[n] = true;
            }
        }
        return G;
    }
    vector<vector<int>> four_times_plus_two_magic_square(int N) {
        assert(N % 4 == 2);
        int NN = N / 2;
        vector<vector<int>> G1 = odd_magic_square(NN);
        for(auto &l : G1)
            for(int &i : l)
                i = (i - 1) * 4;

        vector<vector<int>> G(N, vector<int>(N));

        const vector<vector<int>> L = {
            {4, 1},
            {2, 3}
        }, U = {
            {1, 4},
            {2, 3},
        }, X = {
            {1, 4},
            {3, 2}
        };

        int center = NN / 2;
        for(int y = 0; y < N; y += 2)
            for(int x = 0; x < N; x += 2) {
                vector<vector<int>> A;
                int yy = y / 2, xx = x / 2;
                if(yy <= center) A = L;
                else if(yy == center + 1) A = U;
                else A = X;
                
                if(yy == center and xx == center) A = U;
                if(yy == center + 1 and xx == center) A = L;
                
                rep(dy, 2) rep(dx, 2) {
                    G[y + dy][x + dx] = G1[yy][xx] + A[dy][dx];
                }
            }        
        return G;
    }
    
    
    bool solve() {
        int N; cin >> N;
        vector<vector<int>> ans;
        if(N % 2) {
            ans = odd_magic_square(N);
        } else if(N % 4 == 0) {
            ans = four_times_magic_square(N);
        } else if(N % 4 == 2) {
            ans = four_times_plus_two_magic_square(N);
        } else {
            assert(0); // 2
        }
        rep(y, N) {
            rep(x, N) cout << ans[y][x] << (x == N - 1 ? "\n" : " ");
        }
        return 0;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Solver s;
    s.solve();
    return 0;
}
0