結果

問題 No.331 CodeRunnerでやれ
ユーザー te-shte-sh
提出日時 2017-07-19 16:07:41
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 314 ms / 5,000 ms
コード長 3,658 bytes
コンパイル時間 987 ms
コンパイル使用メモリ 109,356 KB
実行使用メモリ 24,304 KB
平均クエリ数 311.53
最終ジャッジ日時 2023-09-03 15:18:47
合計ジャッジ時間 7,001 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 147 ms
24,012 KB
testcase_01 AC 150 ms
23,496 KB
testcase_02 AC 171 ms
23,620 KB
testcase_03 AC 164 ms
23,980 KB
testcase_04 AC 178 ms
24,012 KB
testcase_05 AC 189 ms
23,520 KB
testcase_06 AC 183 ms
23,640 KB
testcase_07 AC 291 ms
24,000 KB
testcase_08 AC 178 ms
23,380 KB
testcase_09 AC 272 ms
24,304 KB
testcase_10 AC 186 ms
24,040 KB
testcase_11 AC 218 ms
23,512 KB
testcase_12 AC 261 ms
23,512 KB
testcase_13 AC 264 ms
24,012 KB
testcase_14 AC 282 ms
23,352 KB
testcase_15 AC 260 ms
23,608 KB
testcase_16 AC 314 ms
23,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap

const n = 41, unknown = 0, wall = 1, empty = 2;
alias Point!int point;
alias Grid!(int, int) igrid;
alias Grid!(bool, int) bgrid;

void main()
{
  auto g = igrid(n, n);
  auto p = point(n/2, n/2);
  auto d = point(0, -1), r = 0;

  int ask(string c)
  {
    import core.stdc.stdlib;
    if (c != "") {
      writeln(c); stdout.flush();
    }
    auto s = readln.chomp;
    if (s == "Merry Christmas!") exit(0);
    auto r = s.to!int;
    if (r == 20151224) ask("F");
    return r;
  }

  auto setKnown(point p, point d, int r) {
    foreach (i; 1..r+1) g[p + d * i] = empty;
    g[p + d * (r + 1)] = wall;
  }

  auto calcRoute(point p) {
    auto q = DList!(point[])(), v = bgrid(n, n);
    q.insertBack([p]);
    v[p] = true;
    while (!q.empty) {
      auto qi = q.front; q.removeFront();
      foreach (np; v.sibPoints4(qi.back)) {
        if (g[np] == unknown) return qi ~ np;
        if (g[np] == empty && !v[np]) {
          v[np] = true;
          q.insertBack(qi ~ np);
        }
      }
    }
    assert(0);
  }

  auto rot(ref point p, ref point d, point td)
  {
    while (d != td) {
      auto r = ask("L");
      d = d.predSwitch(point(0, -1), point(-1, 0), point(-1, 0), point(0, 1), point(0, 1), point(1, 0), point(1, 0), point(0, -1));
      setKnown(p, d, r);
    }
  }

  auto move(ref point p, ref point d, point[] route)
  {
    foreach (np; route[1..$]) {
      auto td = np - p;
      rot(p, d, td);
      if (g[p + d] == wall) return;
      auto r = ask("F");
      p = p + d;
      setKnown(p, d, r);
    }
  }

  g[p] = empty;
  r = ask("");
  setKnown(p, d, r);

  for (;;) {
    auto route = calcRoute(p);
    move(p, d, route);
  }
}

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

struct Grid(T, U)
{
  import std.algorithm, std.conv, std.range, std.traits, std.typecons;

  const sibs4 = [Point!U(-1, 0), Point!U(0, -1), Point!U(1, 0), Point!U(0, 1)];
  const sibs8 = [Point!U(-1, 0), Point!U(-1, -1), Point!U(0, -1), Point!U(1, -1),
                 Point!U(1, 0), Point!U(1, 1), Point!U(0, 1), Point!U(-1, 1)];

  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 Grid(m.map!(r => r.dup).array); }
  ref pure auto opIndex(Point!U p) { return m[p.y][p.x]; }
  ref pure auto opIndex(size_t y) { return m[y]; }
  ref pure auto opIndex(size_t y, size_t x) const { return m[y][x]; }
  static if (isAssignable!T) {
    auto opIndexAssign(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; }
    auto opIndexOpAssign(string op, V)(V v, Point!U p) { return mixin("m[p.y][p.x] " ~ op ~ "= v"); }
    auto opIndexOpAssign(string op, V)(V v, size_t y, size_t x) { return mixin("m[y][x] " ~ op ~ "= v"); }
  }
  pure auto validPoint(Point!U p) { return p.x >= 0 && p.x < cols && p.y >= 0 && p.y < rows; }
  pure auto points() const { return rows.to!U.iota.map!(y => cols.to!U.iota.map!(x => Point!U(x, y))).joiner; }
  pure auto sibPoints4(Point!U p) { return sibs4.map!(s => p + s).filter!(p => validPoint(p)); }
  pure auto sibPoints8(Point!U p) { return sibs8.map!(s => p + s).filter!(p => validPoint(p)); }
}
0