Golf Tree Walk Problem

Golf Tree Walk Problem — ExecCode Hard DSA Practice

Solve the Golf Tree Walk problem on ExecCode. Free online hard DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.

Problem description

A forest grid: 0 wall, 1 empty, >1 a tree of that height. Start at (0,0). You must cut trees in strictly increasing height order. Between two consecutive trees you walk a shortest 4-directional path through non-zero cells (standing trees are walkable). Return total steps or -1. Return the total walking steps, or -1 if some next tree is unreachable. You are given: forest — grid of integers Return integer steps or -1. You do not need to return to start. Paths may visit cells more than once across different hops.

Examples

Input {'forest': [[1, 2, 3], [0, 0, 4], [7, 6, 5]]}; Output 6. Input {'forest': [[1, 2, 3], [0, 0, 0], [7, 6, 5]]}; Output -1. Input {'forest': [[1]]}; Output 0

Constraints

Cutting does not turn a cell into a wall; it stays walkable If (0,0) is a tree it is cut when its turn comes, after walking 0 steps to it if you start there

Practice Golf Tree Walk free on ExecCode. Browse DSA problems, topic map, and placement guides.