結果

問題 No.217 魔方陣を作ろう
ユーザー masamasa
提出日時 2015-05-26 23:21:57
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,836 bytes
コンパイル時間 618 ms
コンパイル使用メモリ 69,988 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-20 15:20:39
合計ジャッジ時間 1,876 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>

using namespace std;

typedef vector<int>   VI;
typedef vector< VI > VVI;

int n, n2;
VVI ms;

void show(int n, VVI &vec) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			printf("%3d ", vec[i][j]);
		}
		printf("\n");
	}
}

VVI makeOddMS(int len) {
	VVI magic(n, VI(n, 0));
	int x = len / 2;
	int y = 0;
	int len2 = len * len;
	for (int i = 1; i <= len2 ; i++) {
		magic[y][x] = i;
		x = (x - 1 + len) % len;
		y = (y + 1) % len;
		if (y == 0) {
			x = (x + 1) % len;
		}
	}
	return magic;
}

int main() {

	cin >> n;
	n2 = n * n;
	ms.assign(n, vector<int>(n, 0));

	if (n % 2 == 1) {
		ms = makeOddMS(n);
	} else if (n % 4 == 0) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				int x = j % 4;
				int y = i % 4;
				if (x == y || (x + y) % 4 == 3) {
					ms[j][i] = i * n + j + 1;
				} else {
					ms[n - 1 - j][n - 1 - i] = i * n + j + 1;
				}
			}
		}

	} else {
		int a, b, c, d;
		int n_half = n / 2;
		auto ms1 = makeOddMS(n_half);
		for (int i = 0; i < n_half; i++) {
			for (int j = 0; j < n_half; j++) {
				ms1[i][j] = (ms1[i][j] - 1) * 4;
			}
		}

		for (int i = 0; i < n_half; i++) {
			for (int j = 0; j < n_half; j++) {
				if (i == n_half - 1) {
					a = 1;
					b = 4;
					c = 3;
					d = 2;
				} else if ((i == n_half - 2 && j != n_half / 2) ||
				           (i == n_half / 2 && j == n_half / 2)
				           ) {
					a = 1;
					b = 4;
					c = 2;
					d = 3;
				} else {
					a = 4;
					b = 1;
					c = 2;
					d = 3;
				}

				ms[i * 2 + 0][j * 2 + 0] = ms1[i][j] + a;
				ms[i * 2 + 0][j * 2 + 1] = ms1[i][j] + b;
				ms[i * 2 + 1][j * 2 + 0] = ms1[i][j] + c;
				ms[i * 2 + 1][j * 2 + 1] = ms1[i][j] + d;
			}
		}


	}

	show(n, ms);
	return 0;
}
0