結果

問題 No.659 徘徊迷路
ユーザー mamekinmamekin
提出日時 2018-03-25 12:21:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 2,555 bytes
コンパイル時間 1,176 ms
コンパイル使用メモリ 109,500 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-07 11:38:14
合計ジャッジ時間 2,867 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

// 行列の積
template <class T>
vector<vector<T> > matrixProduct(const vector<vector<T> >& x, const vector<vector<T> >& y)
{
    int a = x.size();
    int b = x[0].size();
    int c = y[0].size();
    vector<vector<T> > z(a, vector<T>(c, 0));
    for(int i=0; i<a; ++i){
        for(int j=0; j<c; ++j){
            for(int k=0; k<b; ++k){
                z[i][j] += x[i][k] * y[k][j];
            }
        }
    }
    return z;
}

// 行列の累乗
template <class T>
vector<vector<T> > matrixPower(const vector<vector<T> >& x, int k)
{
    int n = x.size();
    vector<vector<T> > y(n, vector<T>(n, 0));
    for(int i=0; i<n; ++i)
        y[i][i] = 1; // 積の単位元

    vector<vector<T> > z = x;
    while(k > 0){
        if(k & 1)
            y = matrixProduct(y, z);
        z = matrixProduct(z, z);
        k >>= 1;
    }
    return y;
}

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

int main()
{
    int h, w, t;
    int sy, sx, gy, gx;
    cin >> h >> w >> t >> sy >> sx >> gy >> gx;
    int si = sy * w + sx;
    int gi = gy * w + gx;

    vector<string> grid(h);
    for(int i=0; i<h; ++i)
        cin >> grid[i];

    int n = h * w;
    vector<vector<double> > mat(n, vector<double>(n, 0.0));
    for(int y=0; y<h; ++y){
        for(int x=0; x<w; ++x){
            if(grid[y][x] == '#')
                continue;

            int cnt = 0;
            for(int k=0; k<4; ++k){
                int y2 = y + dy[k];
                int x2 = x + dx[k];
                if(grid[y2][x2] != '#')
                    ++ cnt;
            }

            int i = y * w + x;
            if(cnt == 0){
                mat[i][i] = 1.0;
            }
            else{
                for(int k=0; k<4; ++k){
                    int y2 = y + dy[k];
                    int x2 = x + dx[k];
                    if(grid[y2][x2] != '#'){
                        int j = y2 * w + x2;
                        mat[j][i] += 1.0 / cnt;
                    }
                }
            }
        }
    }

    mat = matrixPower(mat, t);
    printf("%.10f\n", mat[gi][si]);

    return 0;
}
0