結果
| 問題 |
No.217 魔方陣を作ろう
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-09-16 14:59:59 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,754 bytes |
| コンパイル時間 | 768 ms |
| コンパイル使用メモリ | 120,420 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-12 04:28:32 |
| 合計ジャッジ時間 | 1,929 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 18 |
ソースコード
import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.regex, std.conv, std.stdio, std.typecons;
void main()
{
auto n = readln.chomp.to!int;
int[][] a;
if (n % 2 == 1)
a = calc1(n);
else if (n % 4 == 0)
a = calc2(n);
else
a = calc3(n);
a.each!(b => writeln(b.map!(to!string).join(" ")));
}
int[][] calc1(int n)
{
auto a = new int[][](n, n);
auto x = n / 2 - 1, y = 1;
auto k = 1;
while (k <= n ^^ 2) {
auto x2 = x + 1;
auto y2 = y - 1;
if (x2 >= n) x2 = 0;
if (y2 < 0) y2 = n - 1;
if (a[y2][x2] > 0) {
y = y + 1;
} else {
x = x2;
y = y2;
}
if (y >= n) y = 0;
a[y][x] = k;
++k;
}
return a;
}
int[][] calc2(int n)
{
auto a = new int[][](n, n);
foreach (i; 0..n)
foreach (j; 0..n)
if (i % 4 == j % 4 || i % 4 + j % 4 == 3)
a[i][j] = i * n + j + 1;
else
a[n - i - 1][n - j - 1] = i * n + j + 1;
return a;
}
int[][] calc3(int n)
{
auto a = new int[][](n, n);
auto m = n / 2;
auto b = calc1(m);
b.each!((l) { l[] -= 1; l[] *= 4; });
foreach (i; 0..m)
foreach (j; 0..m) {
if (i <= m / 2 - 1 || i == m / 2 && j != m / 2 || i == m / 2 + 1 && j == m / 2) {
a[i * 2][j * 2] = b[i][j] + 4;
a[i * 2][j * 2 + 1] = b[i][j] + 1;
} else {
a[i * 2][j * 2] = b[i][j] + 1;
a[i * 2][j * 2 + 1] = b[i][j] + 4;
}
if (i <= m / 2 + 1) {
a[i * 2 + 1][j * 2] = b[i][j] + 2;
a[i * 2 + 1][j * 2 + 1] = b[i][j] + 3;
} else {
a[i * 2 + 1][j * 2] = b[i][j] + 3;
a[i * 2 + 1][j * 2 + 1] = b[i][j] + 2;
}
}
return a;
}