Wednesday, 24 December 2014

Hackerrank Bot saves princess Solution

Problem Statement
Princess Peach is trapped in one of the four corners of a square grid. You are in the center of the grid and can move one step at a time in any of the four directions. Can you rescue the princess?
Input format
The first line contains an odd integer N (3 <= N < 100) denoting the size of the grid. This is followed by an NxN grid. Each cell is denoted by '-' (ascii value: 45). The bot position is denoted by 'm' and the princess position is denoted by 'p'.
Grid is indexed using Matrix Convention
Output format
Print out the moves you will take to rescue the princess in one go. The moves must be separated by '\n', a newline. The valid moves are LEFT or RIGHT or UP or DOWN.
Sample input
3
---
-m-
p--
Sample output
DOWN
LEFT
Task
Complete the function displayPathtoPrincess which takes in two parameters - the integer N and the character array grid. The grid will be formatted exactly as you see it in the input, so for the sample input the princess is at grid[2][0]. The function shall output moves (LEFT, RIGHT, UP or DOWN) on consecutive lines to rescue/reach the princess. The goal is to reach the princess in as few moves as possible.
The above sample input is just to help you understand the format. The princess ('p') can be in any one of the four corners.
Scoring
Your score is calculated as follows : (NxN - number of moves made to rescue the princess)/10, where N is the size of the grid (3x3 in the sample testcase).
Source Code:
 #include <stdio.h>  
 #include <string.h>  
 #include <math.h>  
 void displayPathtoPrincess(int n, char grid[101][101]){  
   //logic here  
   int i =0,j=0,posi=0,posj=0;  
   for(i=0;i<n;i++)  
     {  
     for(j=0;j<n;j++){  
       if(grid[i][j]=='m')  
         {  
         posi = i;  
         posj = j;  
       }  
     }  
   }  
   if(grid[0][0]=='p')  
     {  
     while(posj>0)  
       {  
       posj--;  
       printf("UP\n");  
     }  
     while(posi>0)  
       {  
       posi--;  
       printf("LEFT\n");  
     }  
   }  
   else if(grid[0][n-1]=='p')  
     {  
         while(posj>0)  
       {  
       posj--;  
       printf("UP\n");  
     }  
     while(posi<n-1)  
       {  
       posi++;  
       printf("RIGHT\n");  
     }  
   }  
   else if(grid[n-1][0]=='p')  
     {  
         while(posj<n-1)  
       {  
       posj++;  
       printf("DOWN\n");  
     }  
     while(posi>0)  
       {  
       posi--;  
       printf("LEFT\n");  
     }  
   }  
   else  
     {  
     while(posj<n-1)  
       {  
       posj++;  
       printf("DOWN\n");  
     }  
     while(posi<n-1)  
       {  
       posi++;  
       printf("RIGHT\n");  
     }  
   }  
 }  
 int main()  
 {  
   int m,i;  
   scanf("%d", &m);  
   char grid[101][101];  
   char line[101];  
   for( i=0; i<m; i++) {  
     scanf("%s", line);  
     strcpy(grid[i], line);  
   }  
   displayPathtoPrincess(m,grid);  
   return 0;  
 }  
** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)




1 comment:

  1. #include
    using namespace std;

    #define ll long long int
    #define mod 1000000007



    int main() {
    // your code goes here
    ll n,x,y,i,j,x1,y1,f,t;
    cin>>n;
    char ch;
    for(i=0;i>ch;
    if(ch=='p')
    {
    x=j;
    y=i;
    }
    if(ch=='m')
    {
    x1=j;
    y1=i;
    }
    }
    }
    f=x-x1;
    t=abs(x-x1);
    while(t--)
    {
    if(f<0)
    {
    cout<<"LEFT"<<endl;
    }
    else
    {
    cout<<"RIGHT"<<endl;
    }
    }
    f=y-y1;
    t=abs(y-y1);
    while(t--)
    {
    if(f<0)
    {
    cout<<"UP"<<endl;
    }
    else
    {
    cout<<"DOWN"<<endl;
    }
    }
    return 0;
    }

    ReplyDelete