結果

問題 No.217 魔方陣を作ろう
ユーザー te-shte-sh
提出日時 2016-09-16 14:59:59
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,754 bytes
コンパイル時間 926 ms
コンパイル使用メモリ 111,816 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-09-02 22:15:19
合計ジャッジ時間 2,614 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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;
}
0