結果

問題 No.659 徘徊迷路
ユーザー kimiyukikimiyuki
提出日時 2018-03-03 00:11:09
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 2,481 bytes
コンパイル時間 2,919 ms
コンパイル使用メモリ 208,168 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 03:38:30
合計ジャッジ時間 4,015 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < int(n); ++ (i))
using namespace std;
template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }

const int dy[] = { -1, 1, 0, 0 };
const int dx[] = { 0, 0, 1, -1 };

vector<vector<double> > operator * (vector<vector<double> > const & a, vector<vector<double> > const & b) {
    int n = a.size();
    vector<vector<double> > c = vectors(n, n, double());
    REP (y, n) REP (z, n) REP (x, n) c[y][x] = c[y][x] + a[y][z] * b[z][x];
    return c;
}
vector<double> operator * (vector<vector<double> > const & a, vector<double> const & b) {
    int n = a.size();
    vector<double> c(n);
    REP (y, n) REP (z, n) c[y] = c[y] + a[y][z] * b[z];
    return c;
}
vector<vector<double> > unit_matrix(int n) {
    vector<vector<double> > e = vectors(n, n, double());
    REP (i, n) e[i][i] = 1;
    return e;
}
vector<vector<double> > zero_matrix(int n) {
    vector<vector<double> > o = vectors(n, n, double());
    return o;
}
vector<vector<double> > powmat(vector<vector<double> > x, long long y) {
    int n = x.size();
    auto z = unit_matrix(n);
    for (long long i = 1; i <= y; i <<= 1) {
        if (y & i) z = z * x;
        x = x * x;
    }
    return z;
}

int main() {
    // input
    int h, w, t; scanf("%d%d%d", &h, &w, &t);
    int sy, sx; scanf("%d%d", &sy, &sx);
    int gy, gx; scanf("%d%d", &gy, &gx);
    auto f = vectors(h, w, char());
    REP (y, h) REP (x, w) scanf(" %c", &f[y][x]);

    // solve
    vector<vector<double> > a = zero_matrix(h * w);
    REP (y, h) REP (x, w) {
        if (f[y][x] == '#') continue;
        int cnt = 0;
        REP (dir, 4) {
            int ny = y + dy[dir];
            int nx = x + dx[dir];
            if (f[ny][nx] == '#') continue;
            cnt += 1;
        }
        if (cnt == 0) {
            a[y * w + x][y * w + x] += 1.0;
        } else {
            REP (dir, 4) {
                int ny = y + dy[dir];
                int nx = x + dx[dir];
                if (f[ny][nx] == '#') continue;
                a[ny * w + nx][y * w + x] += 1.0 / cnt;
            }
        }
    }
    vector<double> b(h * w);
    b[sy * w + sx] = 1.0;
    double result = (powmat(a, t) * b)[gy * w + gx];

    // output
    printf("%.16lf\n", result);
    return 0;
}
0