結果

問題 No.217 魔方陣を作ろう
ユーザー hayashitakeruhayashitakeru
提出日時 2018-03-10 04:56:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,353 bytes
コンパイル時間 667 ms
コンパイル使用メモリ 67,948 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-20 00:15:12
合計ジャッジ時間 1,550 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
using namespace std;

int main()
{
	int n;
	cin >> n;

	const int n2 = n * n;

	auto magicSquare = [=](auto eval) {
		for (int y = 0; y < n; ++y)
		{
			for (int x = 0; x < n; ++x)
			{
				cout << eval(x, y);
				if (x < n - 1)
				{
					cout << ' ';
				}
			}
			cout << endl;
		}
	};
	auto evenMagic = [=](int xbase, int stride) {
		return [=](int x, int y) {
			int ybase = ((xbase - x) * 2 + n) % n;
			int yadd = (y - ybase + n) % n * stride;
			return ((xbase - x) * n + n2) % n2 + 1 + yadd;
		};
	};

	if (n & 1)
	{
		magicSquare(evenMagic(n >> 1, n + 1));
	}
	else if (n & 2)
	{
		magicSquare([even = evenMagic(n >> 2, (n >> 1) + 1), halfn = n >> 1](int x, int y) {
			const int halfx = x >> 1;
			const int halfy = y >> 1;
			const bool isSwap = halfx == halfn && (halfy == halfn || halfy == halfn + 1);
			static const int L[][2] = { { 4, 1 },{ 2, 3 } };
			static const int U[][2] = { { 1, 4 },{ 2, 3 } };
			static const int X[][2] = { { 1, 4 },{ 3, 2 } };
			auto m = halfy <= halfn + 1 ? (halfy == halfn + 1) ^ isSwap ? U : L : X;
			const int val = (even(halfx, halfy) - 1) << 2;
			return val + m[y & 1][x & 1];
		});
	}
	else
	{
		magicSquare([n2, i = 0](int x, int y) mutable {
			const bool isDiagonal = (x & 3) == (y & 3) || 3 - (x & 3) == (y & 3);
			return isDiagonal ? ++i : n2 - i++;
		});
	}
	return 0;
}
0