結果

問題 No.659 徘徊迷路
ユーザー te-shte-sh
提出日時 2018-04-16 17:49:29
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 124 ms / 2,000 ms
コード長 2,892 bytes
コンパイル時間 2,106 ms
コンパイル使用メモリ 152,088 KB
実行使用メモリ 6,780 KB
最終ジャッジ日時 2023-09-03 19:36:05
合計ジャッジ時間 3,969 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 25 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 30 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 45 ms
4,380 KB
testcase_09 AC 106 ms
4,376 KB
testcase_10 AC 123 ms
4,764 KB
testcase_11 AC 122 ms
4,704 KB
testcase_12 AC 23 ms
4,380 KB
testcase_13 AC 105 ms
4,376 KB
testcase_14 AC 106 ms
4,380 KB
testcase_15 AC 124 ms
5,076 KB
testcase_16 AC 124 ms
6,780 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(89): Deprecation: usage of the `body` keyword is deprecated. Use `do` instead.
Main.d(96): Deprecation: usage of the `body` keyword is deprecated. Use `do` instead.
Main.d(103): Deprecation: usage of the `body` keyword is deprecated. Use `do` instead.

ソースコード

diff #

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}

void main()
{
  int r, c, t; readV(r, c, t);
  int sx, sy; readV(sx, sy);
  int gx, gy; readV(gx, gy);
  string[] b; readC(r, b);

  auto n = (r-2)*(c-2), a = Matrix!real(n, n);
  auto idx(int i, int j) { return (i-1)*(c-2)+(j-1); }

  foreach (i; 1..r-1)
    foreach (j; 1..c-1) {
      if (b[i][j] == '#') continue;
      auto d = [b[i-1][j], b[i+1][j], b[i][j-1], b[i][j+1]].map!(bij => bij == '.' ? 1 : 0).sum;
      auto id1 = idx(i, j);
      if (d == 0) {
        a[id1][id1] = 1;
      } else {
        if (b[i-1][j] == '.') a[idx(i-1, j)][id1] = 1.0L/d;
        if (b[i+1][j] == '.') a[idx(i+1, j)][id1] = 1.0L/d;
        if (b[i][j-1] == '.') a[idx(i, j-1)][id1] = 1.0L/d;
        if (b[i][j+1] == '.') a[idx(i, j+1)][id1] = 1.0L/d;
      }
    }

  auto v = new real[](n);
  v[] = 0; v[idx(sx, sy)] = 1;

  auto p = repeatedSquare(a, t, Matrix!real.unit(n)) * v;
  writefln("%.7f", p[idx(gx, gy)]);
}

pure T repeatedSquare(alias pred = "a * b", T, U)(T a, U n)
{
  return repeatedSquare!(pred, T, U)(a, n, T(1));
}

pure T repeatedSquare(alias pred = "a * b", T, U)(T a, U n, T init)
{
  import std.functional;
  alias predFun = binaryFun!pred;

  if (n == 0) return init;

  auto r = init;
  while (n > 0) {
    if (n&1) r = predFun(r, a);
    a = predFun(a, a);
    n >>= 1;
  }

  return r;
}

struct Matrix(T)
{
  size_t r, c;
  T[][] a;
  alias a this;

  static ref auto unit(size_t n)
  {
    auto r = Matrix!T(n, n);
    foreach (i; 0..n) r[i][i] = 1;
    return r;
  }

  this(size_t r, size_t c)
  {
    this.r = r; this.c = c;
    a = new T[][](r, c);
    static if (T.init != 0) foreach (i; 0..r) a[i][] = 0;
  }

  this(T[][] b)
  {
    r = b.length;
    c = b[0].length;
    a = b;
  }

  ref auto dup() { auto x = Matrix!T(r, c); foreach (i; 0..r) x[i][] = a[i][]; return x; }

  ref auto opBinary(string op)(Matrix!T b) if (op == "+" || op == "-") in { assert(r == b.r && c == b.c); } body
  {
    auto x = Matrix!T(r, c);
    foreach (i; 0..r) foreach (j; 0..c) x[i][j] = mixin("a[i][j]"~op~"b[i][j]");
    return x;
  }

  ref auto opBinary(string op: "*")(Matrix!T b) in { assert(c == b.r); } body
  {
    auto x = Matrix!T(r, b.c);
    foreach (i; 0..r) foreach (j; 0..b.c) foreach (k; 0..c) x[i][j] += a[i][k]*b[k][j];
    return x;
  }

  ref auto opBinary(string op: "*")(T[] b) in { assert(c == b.length); } body
  {
    auto x = new T[](r);
    static if (T.init != 0) x[] = 0;
    foreach (i; 0..r) foreach (j; 0..c) x[i] += a[i][j]*b[j];
    return x;
  }
}
0