BotSavesPrincess Problem

Discussion in 'Java' started by Alioune, Aug 9, 2017.

  1. Alioune

    Alioune

    Joined:
    Jul 19, 2017
    Messages:
    13
    Likes Received:
    2
    Location:
    Phoenix, AZ
    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

    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.

    Here's the code I wrote for it:
    Code (Text):
    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, Aug 9, 2017
    #1
    1. Advertisements

  2. Alioune

    John Joe

    Joined:
    Apr 25, 2017
    Messages:
    49
    Likes Received:
    8
    Location:
    Malaysia
    John Joe, Aug 9, 2017
    #2
    Ian likes this.
    1. Advertisements

  3. Alioune

    Alioune

    Joined:
    Jul 19, 2017
    Messages:
    13
    Likes Received:
    2
    Location:
    Phoenix, AZ
    It didn't work. I still cannot add input. Also, did you notice that I set
    Code (Text):
    String entry = in.nextLine()
    ?
     
    Alioune, Aug 9, 2017
    #3
  4. Alioune

    John Joe

    Joined:
    Apr 25, 2017
    Messages:
    49
    Likes Received:
    8
    Location:
    Malaysia
    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: Aug 10, 2017
    John Joe, Aug 10, 2017
    #4
  5. Alioune

    Alioune

    Joined:
    Jul 19, 2017
    Messages:
    13
    Likes Received:
    2
    Location:
    Phoenix, AZ
    When you do that and you run the code, can you enter any input? I still can't.
     
    Alioune, Aug 10, 2017
    #5
  6. Alioune

    John Joe

    Joined:
    Apr 25, 2017
    Messages:
    49
    Likes Received:
    8
    Location:
    Malaysia
    Yes,I can. You can use System.out.println() to see the differences.
     
    John Joe, Aug 11, 2017
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Andreas Suurkuusk
    Replies:
    0
    Views:
    4,361
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,678
    Ted Miller
    Sep 13, 2003
  3. Merek
    Replies:
    0
    Views:
    2,257
    Merek
    Dec 3, 2003
  4. Scott Meddows
    Replies:
    1
    Views:
    626
    John Saunders
    Jun 8, 2004
  5. Mustafa Yesilkaya
    Replies:
    0
    Views:
    1,772
    Mustafa Yesilkaya
    Jun 11, 2004
  6. Eric
    Replies:
    0
    Views:
    1,192
  7. Stephen Kellett

    TomCat problem - locale problem?

    Stephen Kellett, Jul 6, 2004, in forum: Java
    Replies:
    4
    Views:
    9,965
    Stephen Kellett
    Jul 8, 2004
  8. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    893
    Bullschmidt
    May 11, 2004
Loading...