Hackerrank code arena challenges

Hackerrank code arena challenges


You are lost in a dense jungle and it is getting dark. There is at least one path that leads you to the city on the other side but you cannot see anything until you are right in front of it as the trees and bushes obscure the path.

Devise an algorithm that is guaranteed to find the way out. Your goal is to go out of the jungle as fast as you can before it gets dark.

Input start with a number N and then the matrix of size N x N filled with S, E, T, and P which is our map. Map contains a single S representing the start point, and single E representing the end point and P representing the path and T representing the Tree.

output single integer i.e. minimum number of moves from S to E.

You can assume that the maps would be in square form and can be up to a maximum size of 30X30. You can move in four directions North East West South.
You can move in any direction when you find P but cannot move to a point where a T is present.

*Problem provided by JDASAMPLE INPUT 




Time Limit: 5.0 sec(s) for each input file.Memory Limit: 256 MBSource Limit: 1024 KB

Solution in cpp :

Basically you need the shortest path using BFS starting from start position to end position.


using namespace std;

int p=0;
struct Point
int x;
int y;

struct queueNode
Point pt; // The cordinates of a cell
int dist; // cell’s distance of from the source

bool isValid(int row, int col,int n )
return (row >= 0) && (row < n) && (col >= 0) && (col < n);

int rowNum[] = {-1, 0, 0, 1};
int colNum[] = {0, -1, 1, 0};

int BFS(int **mat, Point src, Point dest ,int n)
if (!mat[src.x][src.y] || !mat[dest.x][dest.y])
return -1;

bool visited[n][n];


// Driver program to test above function
int main()
int n ;
cin >> n;
char mat1[n][n];
//int mat[n][n];
for(int i =0 ; i < n ; i++) { for(int j =0 ; j < n ; j++){ cin >> mat1[i][j];

bool visited[n][n];

memset(visited, false, sizeof visited);

visited[src.x][src.y] = true;

queue q;
queueNode s = {src, 0};
q.push(s); // Enqueue source cell
while (!q.empty())
queueNode curr = q.front();
Point pt = curr.pt;
if (pt.x == dest.x && pt.y == dest.y)
return curr.dist;

for (int i = 0; i < 4; i++) {
int row = pt.x + rowNum[i]; int col = pt.y + colNum[i];
if (isValid(row, col,n) &&
mat[row][col] &&
!visited[row][col]) {
visited[row][col] = true; queueNode Adjcell = { {row, col}, curr.dist + 1 }; q.push(Adjcell); } } }
return -1;


Prison Break

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×N) cells.i.e The shape of prison was (N×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).

note:->Alfie can move in all four direction{ if his current location is (X,Y), he can move to either 
(X+1,Y)(X−1,Y)(X,Y+1)(X,Y−1) }. If the first cell (1,1) and the last cell(N,N) contain motion detectors,then Alfie can’t break out of the prison.


The first line contain number of test cases “T“.T test cases follow.The first line of each test case contains an integer “N“,(i.e the size of the (N×N) matrix).The next n lines contain N space separated values either 0 or 1.”1” represents a cell containing motion detectors.


output total number of unique possible paths which he can take to escape the prison.




Solution :

public class CA {

// Check if cell (x, y) is valid or not
private static boolean isValidCell(int x, int y,int N)
if (x < 0 || y < 0 || x >= N || y >= N)
return false;

return true;

private static int countPaths(int maze[][], int x, int y,
boolean visited[][], int count, int N)
// if destination (bottom-rightmost cell) is found,
// increment the path count
if (x == N – 1 && y == N – 1 && maze[x][y]!=-1)
return count;

// mark current cell as visited
visited[x][y] = true;
// if current cell is a valid and open cell
if (isValidCell(x, y,N) && maze[x][y] == 0)
// go down (x, y) –> (x + 1, y)
if (x + 1 < N && !visited[x + 1][y])
count = countPaths(maze, x + 1, y, visited, count,N);

// go up (x, y) –> (x – 1, y)
if (x – 1 >= 0 && !visited[x – 1][y])
count = countPaths(maze, x – 1, y, visited, count,N);

// go right (x, y) –> (x, y + 1)
if (y + 1 < N && !visited[x][y + 1])
count = countPaths(maze, x, y + 1, visited, count,N);

// go left (x, y) –> (x, y – 1)
if (y – 1 >= 0 && !visited[x][y – 1])
count = countPaths(maze, x, y – 1, visited, count,N);

// backtrack from current cell and remove it from current path
visited[x][y] = false;

return count;

public static void main (String[] args) throws java.lang.Exception
java.io.BufferedReader r = new java.io.BufferedReader (
new java.io.InputStreamReader (System.in));
Long t1 =Long.parseLong(r.readLine().toString());
Long max , min;
while(t1– > 0) {
Long n =Long.parseLong(r.readLine().toString());
int[][] maze = new int[ n.intValue()][n.intValue()];
for(int i=0 ; i < n ; i++)
int j=0;
String data[] = r.readLine().split(” “, n.intValue());
for(int k=0 ;k < n ;k++) {
maze[i][k] = Integer.parseInt(data[k].trim())==1?-1:0;

int count = 0;
// 2D matrix to keep track of cells involved in current path
boolean[][] visited = new boolean[n.intValue()][n.intValue()];

// start from source cell (0, 0)
count = countPaths(maze, 0, 0, visited, count, n.intValue() );


Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at writeulearn@gmail.com along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.