結果

問題 No.659 徘徊迷路
ユーザー nmnmnmnmnmnmnmnmnmnmnmnmnmnm
提出日時 2017-03-08 00:32:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 2,666 bytes
コンパイル時間 2,089 ms
コンパイル使用メモリ 104,844 KB
実行使用メモリ 6,936 KB
最終ジャッジ日時 2023-09-20 10:58:48
合計ジャッジ時間 3,436 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
6,756 KB
testcase_01 AC 57 ms
6,900 KB
testcase_02 AC 66 ms
6,876 KB
testcase_03 AC 66 ms
6,652 KB
testcase_04 AC 66 ms
6,820 KB
testcase_05 AC 52 ms
6,680 KB
testcase_06 AC 56 ms
6,688 KB
testcase_07 AC 53 ms
6,824 KB
testcase_08 AC 57 ms
6,668 KB
testcase_09 AC 65 ms
6,676 KB
testcase_10 AC 75 ms
6,364 KB
testcase_11 AC 74 ms
6,480 KB
testcase_12 AC 53 ms
6,908 KB
testcase_13 AC 65 ms
6,936 KB
testcase_14 AC 66 ms
6,648 KB
testcase_15 AC 76 ms
6,696 KB
testcase_16 AC 76 ms
6,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define per(i,a,b) for(ll i=(b-1);i>=(a);--i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(c) string(1,c)
#define print(x) cout<<#x<<" = "<<x<<endl;

#define MOD 1000000007
#define N 10

vector<vector<double> > mul(vector<vector<double> > v1, vector<vector<double> > v2){
    vector<vector<double> > ret(v1.size(),vector<double>(v2[0].size(),0));
    for(int i=0;i<v1.size();i++) {
        for(int j=0;j<v2[0].size();j++) {
            for(int k=0;k<v1.size();k++) {
                ret[i][j]+=v1[i][k]*v2[k][j];
            }
        }
    }
    return ret;
}

vector<vector<double> > powmatrix(vector<vector<double> > vv, vector<vector<double> > v, long long n){
    vector<vector<vector<double> > > vvv;
    vvv.push_back(vv);
    for(long long i = 0; i < 32; i++){
        vvv.push_back(mul(vvv[i],vvv[i]));
    }
    vector<vector<double> > v1(vv.size(), vector<double>(vv.size(), 0));
    for(int i = 0; i < vv.size(); i++)v1[i][i] = 1.;
    for(int i = 0; i < 32; i++){
        if((n>>i)&1){
            v1 = mul(v1,vvv[i]);
        }
    }
    v = mul(v1,v);
    return v;
}

long long d[N][N];

int main() {
	ll r,c,t;
  cin>>r>>c>>t;
  ll sy,sx,gy,gx;
  cin>>sy>>sx;
  cin>>gy>>gx;
  vector<string> vs;
  rep(i,0,r){
    string s;
    cin>>s;
    vs.pb(s);
  }
  if(vs[sy][sx]=='#')return 0;
  if(vs[gy][gx]=='#')return 0;
  clr(d,0);
  rep(y,0,vs.sz){
    rep(x,0,vs[0].sz){
      if(vs[y][x]=='#')d[y][x]=1;
    }
  }
  vector<vector<double> > v(N*N, vector<double>(1, 0.));
  v[sy*c+sx][0] = 1.;
  vector<vector<double> > vv(N*N, vector<double>(N*N, 0.));
  int dy[4]={-1,0,0,1};
  int dx[4]={0,-1,1,0};
  rep(y,1,r-1){
    rep(x,1,c-1){
      if(d[y][x]==1)continue;
      int p = 0;
      rep(i,0,4){
        if(d[y+dy[i]][x+dx[i]]==0)p++;
      }
      if(p==0){
        vv[y*c+x][y*c+x] = 1.;
      }
      else{
        rep(i,0,4){
          if(d[y+dy[i]][x+dx[i]]==0){
            vv[(y+dy[i])*c+(x+dx[i])][y*c+x] = 1./p;
          }
        }
      }
    }
  }
  vector<vector<double> > va = powmatrix(vv,v,t);
  printf("%20.20f\n", va[gy*c+gx][0]);
	return 0;
}
0