結果

問題 No.86 TVザッピング(2)
ユーザー te-shte-sh
提出日時 2017-04-17 15:56:14
言語 D
(dmd 2.105.2)
結果
TLE  
実行時間 -
コード長 2,282 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 103,056 KB
実行使用メモリ 8,200 KB
最終ジャッジ日時 2023-09-03 12:50:40
合計ジャッジ時間 8,440 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 487 ms
4,380 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.typecons;  // Tuple, Nullable, BigFlags

void main()
{
  auto rd = readln.split.map!(to!size_t), n = rd[0], m = rd[1];
  auto aij = Matrix!bool(n.iota.map!(_ => readln.chomp.map!(c => c == '.').array).array);

  auto w = aij.joiner.count!"a";

  foreach (s; aij.points!int)
    foreach (di; 0..4) {
      if (calc(aij, s, di, w)) {
        writeln("YES");
        return;
      }
    }

  writeln("NO");
}

auto walk(Matrix!bool bij, point p, point d, point s) {
  auto c = 0;
  for (;;) {
    auto np = p + d;
    if (np == s) return Tuple!(int, point)(c + 1, s);
    if (!bij.validIndex(np) || !bij[np]) return Tuple!(int, point)(c, p);
    p = np;
    bij[p] = false;
    ++c;
  }
}

auto calc(Matrix!bool aij, point s, int di, size_t w) {
  auto bij = aij.dup;
  bij[s] = false;
  auto p = s;

  auto c = 0;
  for (;;) {
    auto d = sibPoints!int[di];

    auto wr = walk(bij, p, d, s), cr = wr[0], pr = wr[1];
    if (cr == 0) return false;

    c += cr;
    p = pr;

    if (p == s && c == w) return true;
    di = (di + 1) % 4;
  }
}

alias Point!int point;

struct Point(T)
{
  T x, y;
  pure auto opBinary(string op: "+")(Point!T rhs) const { return Point!T(x + rhs.x, y + rhs.y); }
}

const sibPoints(T) = [Point!T(-1, 0), Point!T(0, 1), Point!T(1, 0), Point!T(0, -1)];

struct Matrix(T)
{
  import std.traits, std.typecons;

  T[][] m;
  const size_t rows, cols;

  mixin Proxy!m;

  this(size_t r, size_t c) { rows = r; cols = c; m = new T[][](rows, cols); }
  this(T[][] s) { rows = s.length; cols = s[0].length; m = s; }

  pure auto dup() const { return Matrix(m.map!(r => r.dup).array); }
  pure auto opIndex(U)(Point!U p) const { return m[p.y][p.x]; }
  pure auto opIndex(U)(U y) if (!is(U == Point!V, V)) { return m[y]; }
  pure auto opIndex(size_t y, size_t x) const { return m[y][x]; }
  static if (isAssignable!T) {
    auto opIndexAssign(U)(T v, Point!U p) { return m[p.y][p.x] = v; }
    auto opIndexAssign(T v, size_t y, size_t x) { return m[y][x] = v; }
  }
  pure auto validIndex(U)(Point!U p) const { return p.x >= 0 && p.x < cols && p.y >= 0 && p.y < rows; }
  pure auto points(U)() const { return rows.to!U.iota.map!(y => cols.to!U.iota.map!(x => Point!U(x, y))).joiner; }
}
0