import sequtils,strutils,strscans,algorithm,math,future,sets,queues,tables,macros macro unpack*(rhs: seq,cnt: static[int]): auto = let t = genSym(); result = quote do:(let `t` = `rhs`;()) for i in 0..= size : break let child = if R < size and h.compareNode(R,L) <= 0 : R else: L if h.compareNode(i,child) <= 0: break swap(h.nodes[i],h.nodes[child]) i = child proc pushimpl[T](h:var BinaryHeap[T],node:T):void = h.nodes.add(node) #末尾に追加 var i = h.nodes.len() - 1 while i > 0: # 末尾から木を整形 let parent = (i - 1) div 2 if h.compare(h.nodes[parent],node) <= 0: break h.nodes[i] = h.nodes[parent] i = parent h.nodes[i] = node proc popimpl[T](h:var BinaryHeap[T]):T = result = h.nodes[0] # rootと末尾を入れ替えて木を整形 h.nodes[0] = h.nodes[^1] h.nodes.setLen(h.nodes.len() - 1) h.shiftdown() ######################## Binary Heap ############################# let (N,V,sx,sy,gx,gy) = get().split().map(parseInt).unpack(6) L = newSeqWith(N,get().strip().split().map(parseInt)).transpose() proc seek(sx,sy:int, L:seq[seq[int]],diffSeq:seq[tuple[x,y:int]]):auto = type field = tuple[x,y,v,c:int] let (W,H) = (L.len,L[0].len) const INF = int.high div 4 var cost = newSeqWith(W,newSeqWith(H,INF)) var opens = newBinaryHeap[field](proc(a,b:field): int = a.c - b.c) opens.push((sx,sy,0,0)) cost[sx][sy] = 0 while opens.size() > 0: let (x,y,v,c) = opens.pop() if cost[x][y] < v : continue for d in diffSeq: let (nx,ny) = (d.x + x,d.y + y) if nx < 0 or ny < 0 or nx >= W or ny >= H : continue var n_v = v + L[nx][ny] if n_v >= V : continue if nx == gx-1 and ny == gy-1: echo c+1 quit() if n_v < cost[nx][ny] : cost[nx][ny] = n_v opens.push((nx,ny,n_v,c+1)) seek(sx-1,sy-1,L,dxdy4) echo -1