結果
| 問題 |
No.659 徘徊迷路
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-12-31 16:28:32 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 80 ms / 2,000 ms |
| コード長 | 3,045 bytes |
| コンパイル時間 | 21,087 ms |
| コンパイル使用メモリ | 398,108 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-26 13:46:41 |
| 合計ジャッジ時間 | 15,486 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 12 |
ソースコード
fn power_mat(target: &Vec<Vec<f64>>, times: usize) -> Vec<Vec<f64>> {
let n = target.len();
if times == 0 {
let mut ret = vec![vec![0f64; n]; n];
for i in 0..n { ret[i][i] = 1.; }
return ret;
}
if times == 1 {
return target.clone();
}
let temp = power_mat(target, times/2);
if times % 2 == 1 {
calc_mat(&calc_mat(&temp, &temp), target)
} else {
calc_mat(&temp, &temp)
}
}
fn calc_mat(left: &Vec<Vec<f64>>, right: &Vec<Vec<f64>>) -> Vec<Vec<f64>> {
let sizei = left.len();
let sizej = right[0].len();
let sizek = left[0].len();
let mut ret = vec![vec![0f64; sizej]; sizei];
for i in 0..sizei {
for j in 0..sizej {
ret[i][j] = (0..sizek).map(|k| left[i][k] * right[k][j]).sum::<f64>();
}
}
ret
}
fn main() {
let mut temp = String::new();
std::io::stdin().read_line(&mut temp).ok();
let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
let h = temp[0];
let w = temp[1];
let t = temp[2];
let mut start = String::new();
std::io::stdin().read_line(&mut start).ok();
let start: Vec<usize> = start.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
let sx = start[0];
let sy = start[1];
let mut goal = String::new();
std::io::stdin().read_line(&mut goal).ok();
let goal: Vec<usize> = goal.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
let gx = goal[0];
let gy = goal[1];
let grid = (0..h).map(|_| {
let mut temp = String::new();
std::io::stdin().read_line(&mut temp).ok();
let temp = temp.trim();
temp.chars().collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let size = h * w;
let mut mat = vec![vec![0.; size]; size];
for i in 0..h {
for j in 0..w {
if grid[i][j] == '#' { continue; }
let idx = i*w + j;
let u_ok = i > 0 && grid[i-1][j] == '.';
let d_ok = i+1 < h && grid[i+1][j] == '.';
let l_ok = j > 0 && grid[i][j-1] == '.';
let r_ok = j+1 < w && grid[i][j+1] == '.';
let denom = vec![u_ok, d_ok, l_ok, r_ok].iter().filter(|&&v| v).count();
if denom == 0 {
mat[idx][idx] = 1.;
continue;
}
let denom = denom as f64;
if u_ok {
let uidx = idx - w;
mat[idx][uidx] = 1. / denom;
}
if d_ok {
let didx = idx + w;
mat[idx][didx] = 1. / denom;
}
if l_ok {
let lidx = idx - 1;
mat[idx][lidx] = 1. / denom;
}
if r_ok {
let ridx = idx + 1;
mat[idx][ridx] = 1. / denom;
}
}
}
let mat = power_mat(&mat, t);
let sidx = sx * w + sy;
let gidx = gx * w + gy;
println!("{}", mat[sidx][gidx]);
}