結果

問題 No.659 徘徊迷路
ユーザー 0w10w1
提出日時 2018-04-07 01:10:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,865 bytes
コンパイル時間 2,197 ms
コンパイル使用メモリ 207,464 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 18:07:13
合計ジャッジ時間 3,487 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 10 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 16 ms
4,380 KB
testcase_05 WA -
testcase_06 RE -
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 18 ms
4,380 KB
testcase_09 AC 42 ms
4,380 KB
testcase_10 AC 41 ms
4,376 KB
testcase_11 AC 41 ms
4,380 KB
testcase_12 AC 9 ms
4,376 KB
testcase_13 AC 42 ms
4,380 KB
testcase_14 AC 42 ms
4,376 KB
testcase_15 AC 41 ms
4,380 KB
testcase_16 AC 41 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T>
using vec = vector<T>;

template<typename T>
using mat = vec<vec<T>>;

template<typename T>
mat<T> mat_mul(mat<T> a, mat<T> b) {
  mat<T> c(a.size(), vec<T>(b[0].size()));
  for (int i = 0; i < a.size(); ++i) {
    for (int k = 0; k < b.size(); ++k) {
      for (int j = 0; j < b[0].size(); ++j) {
        c[i][j] += a[i][k] * b[k][j];
      }
    }
  }
  return c;
}

template<typename T>
mat<T> mat_pow(mat<T> a, int p) {
  mat<T> r = a;
  for (int i = p - 1; i; i >>= 1) {
    if (i & 1) r = mat_mul(r, a);
    a = mat_mul(a, a);
  }
  return r;
}

signed main() {
  ios::sync_with_stdio(false);
  int R, C, T;
  cin >> R >> C >> T;
  int Sx, Sy, Gx, Gy;
  cin >> Sx >> Sy >> Gx >> Gy;
  vector<string> G(R);
  for (int i = 0; i < R; ++i) {
    cin >> G[i];
  }
  mat<double> f(R * C, vec<double>(R * C));
  for (int i = 0; i < R; ++i) {
    for (int j = 0; j < C; ++j) {
      if (G[i][j] == '#') continue;
      int d = 0;
      for (int di = 0; di < 4; ++di) {
        static const int dx[] = {0, 1, 0, -1};
        static const int dy[] = {1, 0, -1, 0};
        int x = i + dx[di];
        int y = j + dy[di];
        if (x < 0 || x == R || y < 0 || y == C) continue;
        if (G[x][y] == '#') continue;
        ++d;
      }
      if (d) {
        for (int di = 0; di < 4; ++di) {
          static const int dx[] = {0, 1, 0, -1};
          static const int dy[] = {1, 0, -1, 0};
          int x = i + dx[di];
          int y = j + dy[di];
          if (x < 0 || x == R || y < 0 || y == C) continue;
          if (G[x][y] == '#') continue;
          f[i * R + j][x * R + y] = 1.0 / d;
        }
      } else {
        f[i * R + j][i * R + j] = 1.0;
      }
    }
  }
  mat<double> y = mat_pow(f, T);
  cout << fixed << setprecision(9) << y[Sx * R + Sy][Gx * R + Gy] << endl;
  return 0;
}
0