結果

問題 No.659 徘徊迷路
ユーザー finefine
提出日時 2018-03-03 00:03:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 2,529 bytes
コンパイル時間 1,857 ms
コンパイル使用メモリ 168,660 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 02:46:10
合計ジャッジ時間 2,935 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#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;
}
0