結果

問題 No.331 CodeRunnerでやれ
ユーザー te-shte-sh
提出日時 2017-07-19 16:07:03
言語 D
(dmd 2.106.1)
結果
RE  
実行時間 -
コード長 3,658 bytes
コンパイル時間 1,047 ms
コンパイル使用メモリ 122,156 KB
実行使用メモリ 25,604 KB
平均クエリ数 33.24
最終ジャッジ日時 2024-06-12 21:01:28
合計ジャッジ時間 7,664 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 161 ms
25,104 KB
testcase_01 AC 156 ms
24,976 KB
testcase_02 AC 204 ms
24,592 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;
    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