use std::collections::*; use std::io::Write; type Map = BTreeMap; type Set = BTreeSet; type Deque = VecDeque; fn main() { input! { n: usize, h: [[i128; n]; n], s: [bytes; n], a: [[i128; n]; n - 1], b: [[i128; n - 1]; n], q: usize, ask: [(usize1, usize1, i128); q], } let mut ans = vec![std::i128::MIN; n * n]; for sign in 0..2 { let pos = |x: usize, y: usize| x * n + y; let src = n * n; let mut g = vec![vec![]; n * n + 1]; let mut dp = vec![std::i128::MAX / 2; n * n]; for (i, s) in s.iter().enumerate() { for (j, s) in s.iter().enumerate() { let cond = (i + j) & 1 == sign; let v = pos(i, j); if i + 1 < n { let d = a[i][j] * (h[i][j] + h[i + 1][j]).abs(); let u = pos(i + 1, j); g[u].push((v, d)); g[v].push((u, d)); } if j + 1 < n { let d = b[i][j] * (h[i][j] + h[i][j + 1]).abs(); let u = pos(i, j + 1); g[u].push((v, d)); g[v].push((u, d)); } let h = h[i][j]; if *s == b'-' { if cond { dp[v].chmin(h); } else { } } else if *s == b'+' { if cond { } else { dp[v].chmin(-h); } } else if *s == b'=' { if cond { dp[v].chmin(h); } else { dp[v].chmin(-h); } } else { } } } let mut pq = std::collections::BinaryHeap::new(); for i in 0..n { if dp[i] != std::i128::MAX / 2 { pq.push((-dp[i], i)); } } while let Some((d, v)) = pq.pop() { let d = -d; if d > dp[v] { continue; } for &(u, w) in g[v].iter() { if dp[u].chmin(d + w) { pq.push((-dp[u], u)); } } } let mut dp = dp.iter().cloned().take(n * n).collect::>(); /* for i in 0..n { for j in 0..n { if (i + j) & 1 != sign { dp[i * n + j] *= -1; } } } for i in 0..n { for j in 0..n { if i + 1 < n { assert!((dp[pos(i, j)] + dp[pos(i + 1, j)]).abs() <= a[i][j] * (h[i][j] + h[i + 1][j]).abs()); } if j + 1 < n { assert!((dp[pos(i, j)] + dp[pos(i, j + 1)]).abs() <= b[i][j] * (h[i][j] + h[i][j + 1]).abs()); } if s[i][j] == b'-' { assert!(dp[pos(i, j)] <= h[i][j]); } else if s[i][j] == b'+' { assert!(dp[pos(i, j)] >= h[i][j]); } else if s[i][j] == b'=' { assert!(dp[pos(i, j)] == h[i][j]); } else { } } } */ for (i, dp) in dp.iter().enumerate().take(n * n) { if (i / n + i % n) % 2 == sign { ans[i] = *dp; } } } for (a, b, c) in ask { let ans = if ans[a * n + b] >= c {"Yes"} else {"No"}; println!("{}", ans); } } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } #[macro_export] macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } #[macro_export] macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- // ---------- begin chmin, chmax ---------- pub trait ChangeMinMax { fn chmin(&mut self, x: Self) -> bool; fn chmax(&mut self, x: Self) -> bool; } impl ChangeMinMax for T { fn chmin(&mut self, x: Self) -> bool { *self > x && { *self = x; true } } fn chmax(&mut self, x: Self) -> bool { *self < x && { *self = x; true } } } // ---------- end chmin, chmax ----------