結果

問題 No.331 CodeRunnerでやれ
ユーザー te-shte-sh
提出日時 2017-07-19 16:04:39
言語 D
(dmd 2.106.1)
結果
RE  
実行時間 -
コード長 3,643 bytes
コンパイル時間 988 ms
コンパイル使用メモリ 109,360 KB
実行使用メモリ 24,400 KB
平均クエリ数 33.24
最終ジャッジ日時 2023-09-03 15:18:09
合計ジャッジ時間 8,520 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

const n = 11, 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;
    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 = readln.chomp.to!int;
  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