結果

問題 No.223 1マス指定の魔方陣
ユーザー antaanta
提出日時 2015-06-05 23:23:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 2,933 bytes
コンパイル時間 812 ms
コンパイル使用メモリ 86,404 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-20 19:27:04
合計ジャッジ時間 3,345 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

void oddMagicSquare(int N, vector<vi> &a) {
	int i = 0, j = N / 2;
	a.assign(N, vi(N));
	int sqr = N * N;
	rer(k, 1, sqr) {
		a[i][j] = k;
		-- i, ++ j;
		if(k % N == 0) {
			i += 2;
			-- j;
		}else {
			if(j == N) {
				j -= N;
			}else if(i < 0) {
				i += N;
			}
		}
	}
}

void magicSquare(int N, vector<vi> &a) {
	a.assign(N, vi(N));
	if(N % 2 == 1) {
		oddMagicSquare(N, a);
	}else if(N % 4 == 0) {
		int k = 1;
		rep(i, N) rep(j, N)
			a[i][j] = k ++;
		rep(i, N) rep(j, N) if((i + 1) % 4 / 2 == (j + 1) % 4 / 2)
			a[i][j] = N * N + 1 - a[i][j];
	}else {
		int p = N / 2, pp = p * p;
		vector<vi> M;
		oddMagicSquare(p, M);

		rep(i, p) rep(j, p) {
			a[i][j] = M[i][j];
			a[i+p][j] = M[i][j] + 3*pp;
			a[i][j+p] = M[i][j] + 2*pp;
			a[i+p][j+p] = M[i][j] + pp;
		}

		vi I(p, 0), J;
		rep(i, p) I[i] = i+1;
		int k = (N - 2) / 4;
		rep(i, k) J.push_back(i);
		reu(i, N - k + 1, N) J.push_back(i);
		rep(i, p) rep(j, J.size())
			swap(a[i][J[j]], a[i+p][J[j]]);
		swap(a[k][0], a[k+p][0]);
		swap(a[k][k], a[k+p][k]);
	}
}

int main() {
	int N, X, Y, Z;
	while(cin >> N >> X >> Y >> Z) {
		-- X, -- Y;
		vector<vi> a(N, vi(N));
		magicSquare(N, a);
		int y = -1, x = -1;
		rep(i, N) rep(j, N) if(a[i][j] == Z)
			y = i, x = j;
		swap(a[Y], a[y]);
		rep(i, N) swap(a[i][X], a[i][x]);
		int sum = accumulate(all(a[0]), 0);
		while(1) {
			int t = 0, u = 0;
			rep(i, N) {
				t += a[i][i];
				u += a[i][N-1-i];
			}
//			cerr << t <<", " << u << endl;
			if(sum == t && sum == u)
				break;
			int i, j;
			while(1) {
				i = rand() % N, j = rand() % N;
				if(i == Y || j == Y) continue;
				break;
			}
			a[i].swap(a[j]);
		}
		rep(i, N) {
			rep(j, N) {
				if(j != 0) putchar(' ');
				printf("%d", a[i][j]);
			}
			puts("");
		}
	}
	return 0;
}
0