fn power_mat(target: &Vec>, times: usize) -> Vec> { 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>, right: &Vec>) -> Vec> { 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::(); } } ret } fn main() { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = 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 = 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 = 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::>() }) .collect::>(); 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]); }