Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Escape from grid problem solution

YASH PAL, 31 July 202416 February 2026
In this HackerEarth Escape from grid problem solution Assume that you are given a two-dimensional grid G that contains N rows and M columns. The grid consists of only three integers (0, 1, and 2).
  •  0 denotes an empty cell
  •  1 denotes that a cell contains a plant
  •  2 denotes a cell where you are standing initially
You can move into an adjacent cell if that adjacent cell is empty. Two cells are adjacent if they share a side. In other words, if a cell is empty, then you can move in one of the four directions, Up, Down, Left, and Right.
You cannot move out of the grid G.
 
Your task is to find the length of the shortest path to reach one of the boundary edges of the grid without stepping on a plant. The length of a path is equal to the number of moves you make.
 
 
HackerEarth Escape from grid problem solution

 

 

HackerEarth Escape from grid problem solution.

#include <iostream>
#include <queue>

using namespace std;
const int MAX = 1e3 + 5;
int grid[MAX][MAX], dist[MAX][MAX], n, m;
bool vis[MAX][MAX];
int dr[] = {0, 0, -1, 1};
int dc[] = {-1, 1, 0, 0};

int bfs(int fromX, int fromY)
{
queue <pair<int, int> > q;
pair <int, int> p;
dist[fromX][fromY] = 0;
q.push({fromX, fromY});
while(!q.empty())
{
p = q.front();
q.pop();
fromX = p.first;
fromY = p.second;
if(fromX == 0 or fromX == n-1 or fromY == 0 or fromY == m-1)
return dist[fromX][fromY];
for(int i = 0;i < 4;++i)
{
int x = fromX + dr[i];
int y = fromY + dc[i];
if(x >= 0 and x < n and y >= 0 and y < m and grid[x][y] != 1 and vis[x][y] == false)
{
vis[x][y] = true;
dist[x][y] = dist[fromX][fromY] + 1;
q.push({x, y});
}
}
}
return -1;

}

int main()
{
int ans, x, y;
cin >> n >> m;
for(int i = 0;i < n;++i) for(int j = 0;j < m;++j)
{
cin >> grid[i][j];
vis[i][j] = false;
dist[i][j] = 0;
if(grid[i][j] == 2)
x = i, y = j;
}
ans = bfs(x, y);
cout << ans << endl;
return 0;
}
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes