Recent Tutorials and Articles
    HackerRank: AI: Bot saves princess
    Published on: 30th October 2017

    This article provides you with Java solution of "Bot saves princess" problem from Hackerrank AI track.

    Hackerrank Challenge Details

    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'.

    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


    Sample output



    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.

    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).


    Solution Details

    Here is Java based solution to this problem. Idea is to calculate row and column index of both bot and princess, and using the difference of these indexes to calculate the moves. 

    import java.util.*;
    public class Solution {
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            final Scanner scanner = new Scanner(;
            final int n = scanner.nextInt();
            int mR = -1, mC = -1, pR = -1, pC = -1;
            for(int i = 0; i < n; i++) {
                final String row = scanner.nextLine();
                final int mIndex = row.indexOf("m");
                final int pIndex = row.indexOf("p");
                if(mIndex >= 0) {
                    mR = i;
                    mC = mIndex;
                if(pIndex >= 0) {
                    pR = i;
                    pC = pIndex;
            final int noOfUpDownMoves = Math.abs(mR - pR);
            final String upDownMoveType = mR > pR ? "UP" : "DOWN";
            for(int i = 0; i < noOfUpDownMoves; i++) {
            final int noOfLeftRightMoves = Math.abs(mC - pC);
            final String leftRightMoveType = mC > pC ? "LEFT" : "RIGHT";
            for(int i = 0; i < noOfLeftRightMoves; i++) {


    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Published on: 30th October 2017

    Comment Form is loading comments...