結果
| 問題 |
No.659 徘徊迷路
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-08-18 16:19:33 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 209 ms / 2,000 ms |
| コード長 | 2,293 bytes |
| コンパイル時間 | 2,860 ms |
| コンパイル使用メモリ | 203,004 KB |
| 最終ジャッジ日時 | 2025-01-13 03:12:46 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 12 |
ソースコード
//#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;
}