結果

問題 No.659 徘徊迷路
ユーザー SHIJOUSHIJOU
提出日時 2020-08-18 16:19:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 2,293 bytes
コンパイル時間 2,466 ms
コンパイル使用メモリ 203,844 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 07:40:54
合計ジャッジ時間 4,414 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 36 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 53 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 59 ms
5,376 KB
testcase_09 AC 140 ms
5,376 KB
testcase_10 AC 163 ms
5,376 KB
testcase_11 AC 162 ms
5,376 KB
testcase_12 AC 31 ms
5,376 KB
testcase_13 AC 142 ms
5,376 KB
testcase_14 AC 141 ms
5,376 KB
testcase_15 AC 165 ms
5,376 KB
testcase_16 AC 166 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 100010001;
const ll LINF = (ll)INF*INF*10;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

const int N = 10;

//head

int r, c, t;
P S, G;
string s[N];
int di[] = {1, 0, -1, 0};
int dj[] = {0, 1, 0, -1};

using V = vector<ld>;
using M = vector<V>;

inline int num(int i, int j) {return i*c+j;}
inline int num(P x) {return num(x.first, x.second);}

M mul(const M &a, const M b) {
  int n = a.size();
  M res(n, V(n));
  rep(i, n) rep(j, n) rep(k, n) res[i][j] += a[i][k] * b[k][j];
  return res;
}
M pow(const M &a, ll p) {
  int n = a.size();
  if(!p) {
    M res(n, V(n));
    rep(i, n) res[i][i] = 1.0;
    return res;
  }
  M res = pow(a, p>>1);
  res = mul(res, res);
  if(p&1) res = mul(res, a);
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> r >> c >> t;
  cin >> S >> G;
  rep(i, r) cin >> s[i];
  const int x = r*c;
  M mat(x, V(x));
  for(int i = 1; i < r-1; i++) for(int j = 1; j < c-1; j++) {
    if(s[i][j] == '#') continue;
    int cnt = 0;
    rep(k, 4) {
      int ni = i+di[k];
      int nj = j+dj[k];
      if(s[ni][nj] == '#') continue;
      cnt++;
    }
    rep(k, 4) {
      int ni = i+di[k];
      int nj = j+dj[k];
      if(s[ni][nj] == '#') continue;
      mat[num(ni, nj)][num(i, j)] = (ld)1/cnt;
    }
    if(!cnt) {
      mat[num(i, j)][num(i, j)] = 1.0;
    }
  }

  mat = pow(mat, t);
  cout << fixed << setprecision(20) << mat[num(G)][num(S)] << endl;
}
0