use std::collections::BinaryHeap; const DX: [isize; 4] = [1,-1,0,0]; const DY: [isize; 4] = [0,0,1,-1]; fn main() { let mut nvxy = String::new(); std::io::stdin().read_line(&mut nvxy).ok(); let nvxy: Vec = nvxy.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nvxy[0]; let v = nvxy[1]; let ox = nvxy[2]; let oy = nvxy[3]; let ox = if ox > 0 { ox - 1 } else { ox }; let oy = if oy > 0 { oy - 1 } else { oy }; let grid = (0..n).map(|_| { 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(); temp }) .collect::>>(); let mut result = vec![vec![vec![0usize; 2]; n]; n]; let mut pque = BinaryHeap::new(); pque.push((v, 0, 0, 0)); while let Some((vit, x, y, drunk)) = pque.pop() { if vit < result[x][y][drunk] { continue; } for direct in 0..4 { let nx = x as isize + DX[direct]; let ny = y as isize + DY[direct]; if nx >= 0 && ny >= 0 && nx < n as isize && ny < n as isize { let nx = nx as usize; let ny = ny as usize; if grid[nx][ny] >= vit { continue; } if nx == n-1 && ny == n-1 { println!("YES"); return; } let nvit = vit - grid[nx][ny]; let ndrunk = drunk.max(if nx == ox && ny == oy { 1 } else { 0 }); let nvit = if drunk == 0 && nx == ox && ny == oy { nvit * 2 } else { nvit }; if result[nx][ny][ndrunk] < nvit { result[nx][ny][ndrunk] = nvit; pque.push((nvit, nx, ny, ndrunk)); } } } } println!("NO"); }