結果
| 問題 |
No.659 徘徊迷路
|
| ユーザー |
|
| 提出日時 | 2018-03-03 00:03:44 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 16 ms / 2,000 ms |
| コード長 | 2,529 bytes |
| コンパイル時間 | 1,689 ms |
| コンパイル使用メモリ | 170,644 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-22 23:34:14 |
| 合計ジャッジ時間 | 2,072 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 12 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int dh[] = {1, -1, 0, 0};
const int dw[] = {0, 0, 1, -1};
double dp[2][10][10];
int cnt[10][10];
//bool used[10][10];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int r, c, t;
cin >> r >> c >> t;
int sy, sx, gy, gx;
cin >> sy >> sx >> gy >> gx;
vector<string> s(r);
for (int i = 0; i < r; i++) cin >> s[i];
/*
used[sy][sx] = true;
auto judge = [&](){
stack<P> stk;
stk.emplace(sy, sx);
while (!stk.empty()) {
int h = stk.top().first, w = stk.top().second;
stk.pop();
if (h == gy && w == gx) return true;
used[h][w] = true;
for (int j = 0; j < 4; j++) {
int nh = h + dh[j], nw = w + dw[j];
if (nh < 0 || nh >= r || nw < 0 || nw >= c) continue;
if (s[nh][nw] == '#') continue;
if (used[nh][nw]) continue;
stk.emplace(nh, nw);
}
}
return false;
};
if (!judge()) {
cout << 0 << endl;
return 0;
}*/
for (int h = 0; h < r; h++) {
for (int w = 0; w < c; w++) {
for (int j = 0; j < 4; j++) {
int nh = h + dh[j], nw = w + dw[j];
if (nh < 0 || nh >= r || nw < 0 || nw >= c) continue;
cnt[h][w] += (s[nh][nw] == '.');
}
}
}
dp[0][sy][sx] = 1;
if (cnt[sy][sx] == 0) {
cout << setprecision(10) << dp[0][gy][gx] << endl;
return 0;
}
const int MAX_T = 10000;
for (int i = 0; i < min(t, MAX_T); i++) {
for (int h = 0; h < r; h++) {
for (int w = 0; w < c; w++) {
if (s[h][w] == '#') continue;
for (int j = 0; j < 4; j++) {
int nh = h + dh[j], nw = w + dw[j];
if (nh < 0 || nh >= r || nw < 0 || nw >= c) continue;
if (s[nh][nw] == '#') continue;
dp[(i + 1) % 2][nh][nw] += dp[i % 2][h][w] / cnt[h][w];
}
if (i + 1 < min(t, MAX_T)) dp[i % 2][h][w] = 0;
}
}
}
if (t <= MAX_T) cout << setprecision(10) << dp[t % 2][gy][gx] << endl;
else {
if ((t - MAX_T) % 2 == 1) cout << setprecision(10) << dp[1 - MAX_T % 2][gy][gx] << endl;
else cout << setprecision(10) << dp[MAX_T % 2][gy][gx] << endl;
}
return 0;
}