N, V, X, Y = gets.split.take(4).map(&:to_i) L = N.times.map{ gets.split.take(N).map(&:to_i) } DIRS = [[1,0], [0,1], [-1, 0], [0,-1]] $pq = [] $dp = Hash.new(0) def f(start_x, start_y, start_hp) $pq << [start_hp, start_x, start_y, 1] $dp[[start_x, start_y, 1]] = start_hp while !$pq.empty? hp, x, y, oas = $pq.pop if x == N-1 && y == N-1 return true end # p 1 if $dp[[x,y,oas]] != hp # next if $dp[[x,y,oas]] != hp if x == X-1 && y == Y-1 && oas == 1 hp *= 2 oas = 0 end DIRS.each{|dx, dy| _x = x + dx _y = y + dy next if !_x.between?(0, N-1) next if !_y.between?(0, N-1) _hp = hp - L[_y][_x] # if _x == X-1 && _y == Y-1 # p [_x, _y] # p L[_x][_y] # p _hp # end next if _hp <= 0 if $dp[[_x, _y, oas]] < _hp # p [_x, _y] $dp[[_x, _y, oas]] = _hp elem = [_hp, _x, _y, oas] $pq[$pq.bsearch_index{|v|(v<=>elem) > 0}||$pq.size,0] = [elem] # p $pq end } end end puts f(0, 0, V) ? :YES : :NO