use std::collections::*; use std::io::Write; type Map = BTreeMap; type Set = BTreeSet; type Deque = VecDeque; fn main() { input! { n: usize, h: [[i64; n]; n], s: [bytes; n], a: [[i64; n]; n - 1], b: [[i64; n - 1]; n], q: usize, ask: [(usize1, usize1, i64); q], } let mut ans = vec![std::i64::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]; 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 { g[src].push((v, h)); } else { g[v].push((src, h)); } } else if *s == b'+' { if cond { g[v].push((src, -h)); } else { g[src].push((v, -h)); } } else if *s == b'=' { if cond { g[src].push((v, h)); g[v].push((src, -h)); } else { g[src].push((v, -h)); g[v].push((src, h)); } } else { } } } let mut elem = vec![false; n * n + 1]; elem[src] = true; let mut dp = vec![std::i64::MAX / 2; n * n + 1]; dp[src] = 0; let mut deq = std::collections::VecDeque::new(); deq.push_back(src); while let Some(v) = deq.pop_front() { let d = dp[v]; elem[v] = false; for &(u, w) in g[v].iter() { if dp[u].chmin(d + w) && !elem[u] { elem[u] = true; deq.push_back(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 ----------