結果

問題 No.659 徘徊迷路
ユーザー tetsutetsu
提出日時 2018-03-03 00:37:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,067 bytes
コンパイル時間 893 ms
コンパイル使用メモリ 79,216 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 04:49:11
合計ジャッジ時間 2,260 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
#define MAX 1000001
typedef vector<int> vec;
typedef vector<vec> mat;
char map[10][10];
int r, c, t, sx, sy, gx, gy;
int ds[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
mat mul(mat &A, mat &B) {
  mat C(A.size(), vec(B[0].size()));
  for(int i=0; i<A.size(); i++) {
    for(int k=0; k<B.size(); k++) {
      for(int j=0; j<B[0].size(); j++) {
	C[i][j] = (C[i][j]+A[i][k]*B[k][j]);
      }
    }
  }
  return C;
}
void printMat(mat &A) {
  int n = A.size();
  int m = A[0].size();
  for(int i=0; i<n; i++) {
    for(int j=0; j<m; j++) {
      printf("%d ", A[i][j]);
    }
    printf("\n");
  }
}
mat pow(mat A, long n) {
  mat B(A.size(), vec(A.size()));
  for(int i=0; i<A.size(); i++) {
    B[i][i] = 1;
  }
  while(n>0) {
    if(n&1) B = mul(B, A);
    A = mul(A, A);
    n >>= 1;
  }
  return B;
}
double solve() {
  {
    int unko = 0;
    for(int k=0; k<4; k++) {
      int ny = sy+ds[k][0];
      int nx = sx+ds[k][1];
      if(0<=ny && ny < r && 0<=nx && nx < c && map[ny][nx] == '.') unko++;
    }
    //printf("unko=%d, sx=%d, gx=%d, sy=%d, gy=%d\n", unko, sx, gx, sy, gy); // 
    if(unko==0 && sx==gx && sy==gy) return 1;
  }
  mat A(101, vec(101));
  for(int y=0; y<r*c; y++) {
    for(int x=0; x<c*r; x++) {
      A[y][x] = 0;
    }
  }
  //printMat(A);
  for(int y=0; y<r; y++) {
    for(int x=0; x<c; x++) {
      if(map[y][x]=='.') {
	int tmp = 0;
	for(int k=0; k<4; k++) {
	  int ny = y+ds[k][0];
	  int nx = x+ds[k][1];
	  if(0<=ny && ny < r && 0<=nx && nx < c && map[ny][nx] == '.') A[r*y+x][r*ny+nx]=1;
	}
      }
    }
  }
  //printMat(A);
  A = pow(A, t);
  int current = r*sy+sx;
  int cnt = 0;
  for(int i=0; i<r*c; i++) {
    cnt += A[current][i];
  }
  return (double)A[current][gy*r+gx]/(double)cnt;
}
int main() {
  cin >> r >> c >> t >> sy >> sx >> gy >> gx;
  for(int i=0; i<r; i++) {
    scanf("%s", &map[i]);
  }
  // for(int i=0; i<r; i++) {
  //   printf("%s\n", map[i]);
  // }
  cout << solve() << '\n';
  return 0;
}
0