# BotSavesPrincess Problem

#### Alioune

I'm trying to solve a problem from a coding website called hackerrank.com.

Here's the problem:

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

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.

Here's the code I wrote for it:
Code:
``````import java.io.*;
import java.util.*;

public class Solution
{

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

int a = in.nextInt();
int numEntries = 0;
int gridPositionM = 0;
int gridPositionP = 0;
int mPos = 0;
int pPos = 0;

while(numEntries < a)
{
String entry = in.nextLine();

int numDashes = 0;

if(entry.equals("-"))
{
++numDashes;
}

if(entry.equals("m"))
{
mPos = numDashes + 1;
gridPositionM = numEntries;
}

if(entry.equals("p"))
{
pPos = numDashes + 1;
gridPositionP = numEntries;
}
++numEntries;
}

if(gridPositionM < gridPositionP)
{
int pacesDown = gridPositionP - gridPositionM;

while(pacesDown > 0)
{
System.out.println("DOWN");
--pacesDown;
}
}

else if(gridPositionM > gridPositionP)
{
int pacesUp = gridPositionM - gridPositionP;

while(pacesUp > 0)
{
System.out.println("UP");
--pacesUp;
}
}

if(pPos > mPos)
{
int pacesRight = pPos - mPos;

while(pacesRight > 0)
{
System.out.println("RIGHT");
--pacesRight;
}
}

else if(mPos > pPos)
{
int pacesLeft = mPos - pPos;

while(pacesLeft > 0)
{
System.out.println("LEFT");
--pacesLeft;
}
}
}
}``````

This looks to me like it should work very well, but for some odd reason it doesn't work at all. It seems to have something to do with the scanner object because I can't even enter any input. Why is this?

#### Alioune

It didn't work. I still cannot add input. Also, did you notice that I set
Code:
``String entry = in.nextLine()``
?

#### John Joe

The String entry = in.nextLine(); used to get the entry value.
in.nextLine(); used to prevent the skip for the String entry = in.nextLine();

Last edited:

#### Alioune

When you do that and you run the code, can you enter any input? I still can't.

#### John Joe

Yes,I can. You can use System.out.println() to see the differences.

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.