-2

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.

Here is my code:

package challenges;

import java.util.*;

public class Solution {
static void displayPathtoPrincess(int n, int p,String [][] grid){
int botRow=0,botCol=0;

for(int r=0;r<n;r++){
   for (int c = 0; c < grid.length; c++) {
        if(grid[r][c].equals('m')) {
            botRow=r;
            botCol=c;
            continue;
        }
   }
        if(grid[0][0].equals('P')) {
            while(botRow>0) {
                botRow--;
                System.out.println("UP\n");
            }
            while(botCol>0) {
                botCol--;
                System.out.println("LEFT\n");
            }
        }
        else if(grid[0][p-1].equals('P')) {
            while(botRow>0) {
                System.out.println("UP\n");
                botRow--;
            }
            while(botCol<p-1) {
                botCol++;
                System.out.println("RIGHT\n");
            }
        }
        else if(grid[n-1][0].equals('P')) {
            while(botRow<n-1) {
                botRow++;
                System.out.println("DOWN\n");
            }
            while(botCol>0) {
                botCol--;
                System.out.println("LEFT\n");
            }
        }
        else if(grid[n-1][p-1].equals('P')) {
            while(botRow<n-1) {
                botRow++;
                System.out.println("DOWN\n");
            }
            while(botCol<p-1) {
                botCol++;
                System.out.println("RIGHT\n");
            }
        }   
   }
}
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int m,n;
    m = in.nextInt();
    n=m;
    int j=0;
    String grid[][] = new String[m][n];
    for(int i = 0; i < m; i++) {
       while(j<n){
            grid[i][j] = in.next();             
        j++;
       }     
    }

displayPathtoPrincess(m,n,grid);

}
}

Its giving Null Pointer Exception.can anyone please tel what am i doing wrong in here?

srishtiiii
  • 13
  • 1
  • 2

6 Answers6

3

Try this solution:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int m = Integer.parseInt(br.readLine());

        char grid[][] = new char[m][m];
        for(int i = 0; i < m; i++)
        {
            String line = br.readLine();
            grid[i] = (line.trim()).toCharArray();
        }

        displayPathtoPrincess(m, grid);
    }

    static void displayPathtoPrincess(int m, char[][] grid)
    {
        int botRow = 0, botCol = 0, princessRow = 0, princessCol = 0;

        // Get bot and princess coordinates
        for (int r = 0; r < m; r++)
        {
            for (int c = 0; c < grid.length; c++)
            {
                if (grid[r][c] == 'm' || grid[r][c] == 'M')
                {
                    botRow = r;
                    botCol = c;
                }
                else if (grid[r][c] == 'p' || grid[r][c] == 'P')
                {
                    princessRow = r;
                    princessCol = c;
                }
            }
        }

        // Move the bot up or down till bot reaches same row
        if( princessRow < botRow )
        {
            while(botRow != princessRow)
            {
                botRow--;
                System.out.println("UP");
            }
        }
        else if( princessRow > botRow )
        {
            while(botRow != princessRow)
            {
                botRow++;
                System.out.println("DOWN");
            }
        }

        // Move the bot left or right till bot reaches same column
        if( princessCol < botCol )
        {
            while(botCol != princessCol)
            {
                botCol--;
                System.out.println("LEFT");
            }
        }
        else if( princessCol > botCol )
        {
            while(botCol != princessCol)
            {
                botCol++;
                System.out.println("RIGHT");
            }
        }

    }
}
Ashwin
  • 7,277
  • 1
  • 48
  • 70
2

Ok, no offence but this code is a mess.

I know what I will answer won't be exactly what you want, but it might be very helpful for you in the future.

What would I change? (After you change all of this, the error will more likely become apparent or it will be clear enough to ask for help)

First: Variable types.

This is just a tiny detail, but I don't like the way you did it; why use a String if every cell will be represented by a char?

Every time you create a variable (or an Array, or anything at all) think about what you need it to store, and create the variable in a way it will store what you need. If you needed it to store if something is true or false, you wouldn't create a String variable and store "true" or "false", you would use a boolean.

Apply this every time and you will improve faster.

Second: use functions.

Functions are a great way to abstract yourself from the implementation details.

I mean, you can easily see the difference between something like your code, and something like:

static void displayPathtoPrincess(int n, int p,char [][] grid){

    Point bot;
    Point princess;

    getInitialPositions(bot, princess, grid);

    Point dif = getRelativePrincessPosition(bot, princess);

    while (!bot.equals(princess)) {
        switch (dif.y) {
            case UP:
                move (UP_MOVEMENT);
                break;
            case DOWN:
                move (DOWN_MOVEMENT);
                break;
        }
        switch (dif.x) {
            case LEFT:
                move(LEFT_MOVEMENT);
                break;
            case RIGHT:
                move(RIGHT_MOVEMENT);
                break;
        }
    }
}

(And then implement the necessary methods, and create the constants, that is something pretty easy to do)

Third: use the appropriate loop every time.

If you know you want to go from j = 0, while j < n, increasing j every iteration; that's not called a while, that's called a for. (Also, as someone else commented in their answer, you forgot to restart the j; so it only goes in once.

Finally: Let's go with your error now.

I believe your code might be pretty buggy and not give you the desired output, but the NullPointerException is something more specific.

Since you don't use the appropriate loop in the main, for j, and you forgot to restart it's value, you didn't fill the whole array.

When you try to read a value you didn't fill (in the for where you find the robot's position), that value is null, and the value for some rows will be null too; hence the NullPointerException (because you try to access the value of a null array).

Community
  • 1
  • 1
dquijada
  • 1,697
  • 3
  • 14
  • 19
  • hi...thanks a lot for your reply. but actually this is the format given in hackerearth. hence i have to use the same function. Also i tried restarting the loop. And i have initially tried this using nested for loop instead of while. even then i was getting null pointer exception – srishtiiii Apr 11 '16 at 11:30
0

While taking input you have to make j =0 every time you are coming out of while loop as

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int m,n;
    m = in.nextInt();
    n=m;
    int j=0;
    String grid[][] = new String[m][n];
    for(int i = 0; i < m; i++) {
       j = 0;
       while(j<n){
            grid[i][j] = in.next();             
        j++;
       }     
    }

i remains 0 due to this mistake. You should put print statement to check why are you getting error.

js_248
  • 2,032
  • 4
  • 27
  • 38
0

here is the solution..

import java.util.*;

public class Solution {
static void displayPathtoPrincess(int n, int p, String[][] grid) {
    int botRow = 0, botCol = 0;

    for (int r = 0; r < n; r++) {
        for (int c = 0; c < grid.length; c++) {
            if (grid[r][c].equalsIgnoreCase("m")) {
                botRow = r;
                botCol = c;
            }
        }
        if (grid[0][0].equalsIgnoreCase("P")) {
            while (botRow > 0) {
                botRow--;
                System.out.println("UP\n");
            }
            while (botCol > 0) {
                botCol--;
                System.out.println("LEFT\n");
            }
        } else if (grid[0][p - 1].equalsIgnoreCase("P")) {
            while (botRow > 0) {
                botRow--;
                System.out.println("UP\n");
            }
            while (botCol < p - 2) {
                botCol++;
                System.out.println("RIGHT\n");
            }
        } else if (grid[n - 1][0].equalsIgnoreCase("P")) {
            while (botRow < n - 2) {
                botRow++;
                System.out.println("DOWN\n");
            }
            while (botCol > 0) {
                botCol--;
                System.out.println("LEFT\n");
            }
        } else if (grid[n - 1][p - 1].equalsIgnoreCase("P")) {
            while (botRow < n - 2) {
                botRow++;
                System.out.println("DOWN\n");
            }
            while (botCol < p - 2) {
                botCol++;
                System.out.println("RIGHT\n");
            }
        }
    }
}
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int m,n;
    m = in.nextInt();

    int j;
    String grid[][] = new String[m][m];
    for(int i = 0; i < m; i++) {

       for(j=0;j<m;j++){
            grid[i][j] = in.next();             

       }     
    }

displayPathtoPrincess(m,m,grid);

}
}
srishtiiii
  • 13
  • 1
  • 2
0

You might wish to take a look at the following code in Java:

import java.util.Scanner;

public class Solution {

    /*
     * 
     */
    static void printPath(int n, String moves) {
        for (int i = n / 2; i >= 0; i -= 2) {
            System.out.println(moves);
        }           
    }
    
    /*
     * 
     */
    static void displayPathtoPrincess(int n, String [] grid){

        // **** princess @ top left  ****
        
        if (grid[0].substring(0, 1).equals("p")) {
            printPath(n, "UP\nLEFT");
        }
        
        // **** princess @ top right ****
        
        else if (grid[0].substring(n - 1, n).equals("p") ) {
            printPath(n, "UP\nRIGHT");
        }
        
        // **** princess @ bottom right ****
            
        else if (grid[n - 1].substring(n - 1, n).equals("p")) {
            printPath(n, "DOWN\nRIGHT");
        }
        
        // **** princess @ bottom left ****
        
        else {
            printPath(n, "DOWN\nLEFT");
        }
    }

    /*
     * Test code
     */
    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
        int m;
        m = in.nextInt();
        String grid[] = new String[m];
        
        for(int i = 0; i < m; i++) {
            grid[i] = in.next();
        }

        // **** ****
        
        in.close();
        
        // **** ****
        
        displayPathtoPrincess(m,grid);
    }
}
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
0
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

static void nextMove(int n, int r, int c, String  [] grid){

int xcord=0;
int ycord=0;
for ( int i = 0 ; i <n-1; i++){
    String s = grid[i];
    xcord=i;
    for(int x =0; x<=n-1;x++){
        if(s.charAt(x)=='p'){
            ycord=x;
            
            if(xcord==r){
                if(ycord>c){System.out.println("RIGHT");return;}
                else {System.out.println("LEFT");return;}
                
            }else if(xcord<r){System.out.println("UP");return;}
           
            else{ System.out.println("DOWN");return;}
              
            
        }
        
        
    }
  

 }

 System.out.println(xcord);
System.out.println(ycord);
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n,r,c;
    n = in.nextInt();
    r = in.nextInt();
    c = in.nextInt();
    in.useDelimiter("\n");
    String grid[] = new String[n];


    for(int i = 0; i < n; i++) {
        grid[i] = in.next();
    }

  nextMove(n,r,c,grid);

  }
}