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
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Prison Break problem solution

YASH PAL, 31 July 202417 February 2026
In this HackerEarth Prison Break problem solution Alfie was a prisoner in mythland. Though Alfie was a witty and intelligent guy.He was confident of escaping prison.After few days of observation,He figured out that the prison consists of (N X N) cells.i.e The shape of prison was (N X N) matrix.
 
Few of the cells of the prison contained motion detectors.So Alfie planned that while escaping the prison he will avoid those cells containing motion detectors.Yet before executing his plan,Alfie wants to know the total number of unique possible paths which he can take to escape the prison.Initially Alfie is in cell (1, 1) while the exit of the cell (N, N).
 
 
HackerEarth Prison Break problem solution

 

 

HackerEarth Prison Break problem solution.

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int cell[20][20],mark[20][20],count_route=0,n;

void route(int i,int j){
if((i==n)&&(j==n)){
count_route++;
return;
}
mark[i][j] = 1;
if( (j+1)<=n && mark[i][j+1] == 0 && cell[i][j+1] == 0 )
route(i,j+1);
if( (i+1)<=n && mark[i+1][j] == 0 && cell[i+1][j] == 0 )
route(i+1,j);
if( (j-1)>=1 && mark[i][j-1] == 0 && cell[i][j-1] == 0 )
route(i,j-1);
if( (i-1)>=1 && mark[i-1][j] == 0 && cell[i-1][j] == 0 )
route(i-1,j);
mark[i][j] = 0;
return;
}

int main()
{
int i,j,t;
cin >> t;
while(t--){
cin >> n;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
cin >> cell[i][j];
mark[i][j] = 0;
}
}
count_route = 0;
route(1,1);
cout << count_route << 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