結果

問題 No.359 門松行列
ユーザー te-shte-sh
提出日時 2017-07-12 13:44:16
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,458 bytes
コンパイル時間 927 ms
コンパイル使用メモリ 97,268 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 15:10:16
合計ジャッジ時間 1,761 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto t = readln.chomp.to!size_t;
  foreach (_; 0..t) calc();
}

auto calc()
{
  auto l = readln.chomp.to!int;
  auto a = 3.iota.map!(_ => readln.split.to!(int[])).array;

  int[] ex, ey, b = [0, l/2, (l+1)/2, l];
  foreach (i; 0..3)
    foreach (j; 0..3)
      if (a[i][j] == 0) {
        ex ~= j;
        ey ~= i;
      } else {
        b ~= [a[i][j], l - a[i][j]];
      }

  b = b.filter!(bi => bi >= 0 && bi <= l).array.sort().uniq.array;

  auto r = 0, bl = b.length;
  foreach (i; 0..bl) {
    if (i > 0 && b[i] - b[i-1] > 1) {
      a[ey[0]][ex[0]] = b[i] - 1;
      a[ey[1]][ex[1]] = l - b[i] + 1;
      if (isKadomatsuMatrix(a)) r += b[i] - b[i-1] - 1;
    }
    if (i > 0 && i < bl-1) {
      a[ey[0]][ex[0]] = b[i];
      a[ey[1]][ex[1]] = l - b[i];
      if (isKadomatsuMatrix(a)) ++r;
    }
  }

  writeln(r);
}

const ps = [[[0,0],[0,1],[0,2]],[[1,0],[1,1],[1,2]],[[2,0],[2,1],[2,2]],
            [[0,0],[1,0],[2,0]],[[0,1],[1,1],[2,1]],[[0,2],[1,2],[2,2]],
            [[0,0],[1,1],[2,2]],[[2,0],[1,1],[0,2]]];

auto isKadomatsuMatrix(int[][] a)
{
  foreach (p; ps) {
    auto v1 = a[p[0][0]][p[0][1]],
         v2 = a[p[1][0]][p[1][1]],
         v3 = a[p[2][0]][p[2][1]];
    if (!isKadomatsu(v1, v2, v3)) return false;
  }
  return true;
}

auto isKadomatsu(int a, int b, int c)
{
  return a != b && b != c && c != a && (a > b && c > b || a < b && c < b);
}
0