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

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





answer:




Here is a function in C that finds the shortest path from the center of a square grid to one of the four corners, where the size of the grid is given by n and the grid is represented by the character array grid:

void displayPathtoPrincess(int n, char grid[101][101]) { int row = 0, col = 0; // find the position of the princess for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] == 'p') { row = i; col = j; } } } // move up or down to the same row as the princess while (row > n / 2) { printf("UP\n"); row--; } while (row < n / 2) { printf("DOWN\n"); row++; } // move left or right to the same column as the princess while (col > n / 2) { printf("LEFT\n"); col--; } while (col < n / 2) { printf("RIGHT\n"); col++; } }

This function starts by finding the position of the princess in the grid. It then moves up or down to the same row as the princess, and then moves left or right to the same column as the princess. It prints the moves as it goes along.

For example, if you call displayPathtoPrincess(3, [['-', '-', '-'], ['-', 'm', '-'], ['p', '-', '-']]), the function will print the following output:

DOWN LEFT

This is the shortest path from the center of the grid to the corner where the princess is located.

Post a Comment

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();