Clearing the Forest Problem Statement

Ninja lives in a city called Byteland where a festive event is being organized. To make space for this event, Ninja is tasked with clearing a nearby forest. The forest is represented as an N*M grid called FOREST, where each cell has:

  • 0: The cell is empty; Ninja can pass through it.
  • 'X': A tree with height X > 0; Ninja can pass through it.
  • -1: The cell is not accessible.

Ninja starts at the top-left corner cell(0, 0) and must cut down all trees, moving in any of the four cardinal directions per step. Trees must be cut in order from shortest to tallest, turning the cell into 0 after cutting. Ninja also has the option to pass through a tree cell without cutting it. No two trees have the same height, and there is at least one tree to cut.

Your task is to calculate the minimum number of steps Ninja needs to cut down all trees. If it's impossible to cut all trees, return -1.

Input:

The first line contains an integer T denoting the number of test cases. For each test case:

  • An integer line with N and M, the dimensions of FOREST.
  • N subsequent lines each containing M space-separated integers defining the FOREST grid.

Output:

For each test case, output a single integer representing the minimum steps to cut all trees, or -1 if not possible. Each result is on a new line.

Example:

Input:
1
3 3
1 2 0
-1 5 -1
0 6 7
Output:
4

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N, M ≤ 50
  • -1 ≤ FOREST[i][j] ≤ 10^5
  • Time limit: 1 second

Note:

No printing is needed; just implement the function to return the required answer.

AnswerBot
4mo

Calculate the minimum number of steps Ninja needs to cut down all trees in a forest grid.

  • Iterate through the grid to find the shortest path to cut down all trees in order.

  • Use a priority queue to keep ...read more

Help your peers!
Select
Add answer anonymously...

Tower Research Capital LLC Software Developer interview questions & answers

A Software Developer was asked 9mo agoQ. Design an application similar to MakeMyTrip.
A Software Developer was asked Q. Check If Two Nodes Are Cousins You are given an arbitrary binary tree consisting...read more
A Software Developer was asked Q. Counting Nodes in a Complete Binary Tree - Problem Statement Given the root of a...read more

Popular interview questions of Software Developer

A Software Developer was asked 9mo agoQ1. Design an application similar to MakeMyTrip.
A Software Developer was asked Q2. Check If Two Nodes Are Cousins You are given an arbitrary binary tree consisting...read more
A Software Developer was asked Q3. Counting Nodes in a Complete Binary Tree - Problem Statement Given the root of a...read more
Tower Research Capital LLC Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits