結果

問題 No.217 魔方陣を作ろう
ユーザー data9824data9824
提出日時 2015-05-26 23:16:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,133 bytes
コンパイル時間 949 ms
コンパイル使用メモリ 69,440 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-20 15:16:38
合計ジャッジ時間 1,817 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

vector<vector<int> > odd(int n) {
	vector<vector<int> > a(n, vector<int>(n));
	int y = 0;
	int x = n / 2;
	for (int i = 1; i <= n * n; ++i) {
		a[y][x] = i;
		int nextY = (y + n - 1) % n;
		int nextX = (x + 1) % n;
		if (a[nextY][nextX] == 0) {
			y = nextY;
			x = nextX;
		} else {
			y = (y + 1) % n;
		}
	}
	return a;
}

int main() {
	int n;
	cin >> n;
	vector<vector<int> > a(n, vector<int>(n));
	if (n % 2 == 1) {
		a = odd(n);
	} else if (n % 4 == 0) {
		int i = 1;
		for (int y = 0; y < n; ++y) {
			for (int x = 0; x < n; ++x) {
				if ((y + 1) % 4 < 2) {
					if ((x + 1) % 4 < 2) {
						a[y][x] = i;
					}
				} else {
					if ((x + 1) % 4 >= 2) {
						a[y][x] = i;
					}
				}
				++i;
			}
		}
		i = 1;
		for (int y = n - 1; y >= 0; --y) {
			for (int x = n - 1; x >= 0; --x) {
				if ((y + 1) % 4 < 2) {
					if ((x + 1) % 4 >= 2) {
						a[y][x] = i;
					}
				} else {
					if ((x + 1) % 4 < 2) {
						a[y][x] = i;
					}
				}
				++i;
			}
		}
	} else if (n % 4 == 2) {
		int smaller = (n / 4) * 2 + 1;
		vector<vector<int> > base = odd(smaller);
		for (int y = 0; y < smaller; ++y) {
			for (int x = 0; x < smaller; ++x) {
				base[y][x] = (base[y][x] - 1) * 4;
			}
		}
		enum Type {
			U, L, X
		};
		vector<vector<Type> > types(smaller, vector<Type>(smaller, L));
		for (int x = 0; x < smaller; ++x) {
			types[smaller / 2 + 1][x] = U;
		}
		for (int y = smaller / 2 + 2; y < smaller; ++y) {
			for (int x = 0; x < smaller; ++x) {
				types[y][x] = X;
			}
		}
		types[smaller / 2][smaller / 2] = U;
		types[smaller / 2 + 1][smaller / 2] = L;
		int offsetL[2][2] = { 4, 1, 2, 3 };
		int offsetU[2][2] = { 1, 4, 2, 3 };
		int offsetX[2][2] = { 1, 4, 3, 2 };
		for (int y = 0; y < n; ++y) {
			for (int x = 0; x < n; ++x) {
				Type type = types[y / 2][x / 2];
				a[y][x] = base[y / 2][x / 2]
					+ (type == L ? offsetL[y % 2][x % 2]
					: type == U ? offsetU[y % 2][x % 2]
					: offsetX[y % 2][x % 2]);
			}
		}
	}
	for (int y = 0; y < n; ++y) {
		for (int x = 0; x < n; ++x) {
			cout << a[y][x] << " ";
		}
		cout << endl;
	}
	return 0;
}
0