結果
| 問題 |
No.659 徘徊迷路
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-03-02 22:51:11 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 102 ms / 2,000 ms |
| コード長 | 1,959 bytes |
| コンパイル時間 | 12,590 ms |
| コンパイル使用メモリ | 405,208 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-22 04:45:36 |
| 合計ジャッジ時間 | 14,580 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 12 |
ソースコード
type Mat = Vec<Vec<f64>>;
fn main() {
let h: usize = input();
let w: usize = input();
let t: usize = input();
let sy: usize = input();
let sx: usize = input();
let gy: usize = input();
let gx: usize = input();
let mut g: Vec<Vec<u8>> = vec![Vec::new(); h];
for i in 0..h {
g[i] = input::<String>().into_bytes();
}
let g = g;
let n: usize = h * w;
let mut mat: Mat = vec![vec![0.0; n]; n];
for i in 1..h - 1 {
for j in 1..w - 1 {
let mut cnt = 0;
for &(ni, nj) in &[(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)] {
if g[ni][nj] == b'.' {
cnt += 1;
}
}
if cnt > 0 {
for &(ni, nj) in &[(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)] {
if g[ni][nj] == b'.' {
mat[ni * w + nj][i * w + j] += 1.0 / (cnt as f64);
}
}
} else {
mat[i * w + j][i * w + j] = 1.0;
}
}
}
mat = matpow(mat, t);
println!("{:20}", mat[gy * w + gx][sy * w + sx]);
}
fn matpow(mut a: Mat, mut b: usize) -> Mat {
let n: usize = a.len();
let mut res: Mat = vec![vec![0.0; n]; n];
for i in 0..n {
res[i][i] = 1.0;
}
while b > 0 {
if b % 2 == 1 {
res = matmul(&res, &a);
}
a = matmul(&a, &a);
b >>= 1;
}
res
}
fn matmul(a: &Mat, b: &Mat) -> Mat {
let n = a.len();
let mut res = vec![vec![0.0; n]; n];
for i in 0..n {
for k in 0..n {
for j in 0..n {
res[i][j] += a[i][k] * b[k][j];
}
}
}
res
}
fn input<T: std::str::FromStr>() -> T {
use std::io::Read;
let stdin = std::io::stdin();
let stdin = stdin.bytes();
let token = stdin
.skip_while(|x| (*x.as_ref().unwrap() as char).is_whitespace())
.take_while(|x| !(*x.as_ref().unwrap() as char).is_whitespace())
.map(|x| x.unwrap())
.collect();
let token = String::from_utf8(token).unwrap();
match token.parse() {
Ok(x) => x,
Err(_) => panic!("{}", token),
}
}