結果
| 問題 |
No.659 徘徊迷路
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-03-03 00:11:09 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 47 ms / 2,000 ms |
| コード長 | 2,481 bytes |
| コンパイル時間 | 2,450 ms |
| コンパイル使用メモリ | 203,616 KB |
| 最終ジャッジ日時 | 2025-01-05 09:03:17 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 12 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:43:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
43 | int h, w, t; scanf("%d%d%d", &h, &w, &t);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:44:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
44 | int sy, sx; scanf("%d%d", &sy, &sx);
| ~~~~~^~~~~~~~~~~~~~~~~~
main.cpp:45:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
45 | int gy, gx; scanf("%d%d", &gy, &gx);
| ~~~~~^~~~~~~~~~~~~~~~~~
main.cpp:47:32: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
47 | REP (y, h) REP (x, w) scanf(" %c", &f[y][x]);
| ~~~~~^~~~~~~~~~~~~~~~~
ソースコード
#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;
}