結果

問題 No.217 魔方陣を作ろう
ユーザー Yang33Yang33
提出日時 2018-08-23 18:46:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,425 bytes
コンパイル時間 1,588 ms
コンパイル使用メモリ 174,360 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-27 06:29:06
合計ジャッジ時間 3,070 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/08/23  Problem: yukicoder 217  / Link: http://yukicoder.me/problems/no/217  ----- */
/* ------問題------

N 行 N 列の魔方陣を1つ作ってください。
一般に N 行 N 列の魔方陣は複数存在しますが、どれでも構いません。

この問題において、 N 行 N 列の魔方陣とは、
1 から N2 までの整数を N 行 N 列に並べたものであって、
各行、各列、及び2つの対角線それぞれについて、そこに並んだ数の総和がいずれも等しくなるもののことをいいます。

-----問題ここまで----- */
/* -----解説等-----

奇数、4の倍数、偶数をwikipediaを見て書く [リンク](http://ja.wikipedia.org/wiki/%E9%AD%94%E6%96%B9%E9%99%A3)

----解説ここまで---- */

void rotate2d(VVI& a) { // rorateToR
	VVI res(a.size(), VI(a[0].size()));
	FOR(i, 0, a.size())	FOR(j, 0, a[0].size())res[j][a[0].size() - i - 1] = a[i][j];
	a = res;
}
void checker(VVI a) {
	int n = (int)a.size();
	int rightval = n * (n*n + 1) / 2;
	int f = 1;
	FOR(_, 0, 2) {
		FOR(i, 0, n) {
			f &= (rightval == accumulate(ALL(a[i]), 0));
		}
		int sum = 0;
		FOR(i, 0, n) {
			sum += a[i][i];
		}
		f &= (sum == rightval);
		rotate2d(a);
	}

	cout << (f ? "TRUE" : "FALSE") << endl;
}


void odd(VVI &a, int N) { // 斜めにやる
	FOR(i, 0, N) {
		FOR(j, 0, N) {
			int x = N / 2 - i + j, y = j * 2 - x;
			x = (x + N) % N;
			y = (y + N) % N;
			a[y][x] = i * N + j + 1;
		}
	}
}

void four(VVI &a, int N) {
	FOR(i, 0, N) {
		FOR(j, 0, N) {
			if ((i - j + N) % 4 == 0 || (i + j) % 4 == 3) a[i][j] = i * N + j + 1;
			else a[i][j] = N * N - (i*N + j);
		}
	}
}

void even(VVI &a, int N) {
	const int d[3][2][2] = { { { 4,1 },{ 2,3 } },{ { 1,4 },{ 2,3 } },{ { 1,4 },{ 3,2 } } };
	VVI b(N / 2, VI(N / 2));
	odd(b, N / 2);
	FOR(i, 0, N / 2)FOR(j, 0, N / 2) {
		b[i][j] = (b[i][j] - 1) * 4;
		int p = 0;
		if (i > N / 2 / 2 + 1) p = 2;
		if (i == N / 2 / 2 + 1) p = 1;
		if (i == N / 2 / 2 + 1 && j == N / 2 / 2) p = 0;
		if (i == N / 2 / 2 && j == N / 2 / 2) p = 1;
		FOR(ai, 0, 2)FOR(aj, 0, 2) {
			a[i * 2 + ai][j * 2 + aj] = b[i][j] + d[p][ai][aj];
		}
	}
}
VVI makeMagicSquare(int N) {
	VVI a(N, VI(N));
	if (N & 1)odd(a, N);
	else if (N % 4 == 0)four(a, N);
	else even(a, N);
	return a;
}


int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int N; cin >> N;
	//FOR(N, 3, 21) {
		VVI ans = makeMagicSquare(N);
		//checker(ans);
	//}
	FOR(i, 0, N) {
		FOR(j, 0, N) {
			cout << ans[i][j] << " ";
		}cout << endl;
	}

	return 0;
}
0