We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Interview Preparation Kit
  3. Stacks and Queues
  4. Castle on the Grid

Castle on the Grid

Problem
Submissions
Leaderboard
Discussions
Editorial
  1. Prepare
  2. Interview Preparation Kit
  3. Stacks and Queues
  4. Castle on the Grid
Exit Full Screen View
  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal.

Example.





The grid is shown below:

...
.X.
...

The starting position so start in the top left corner. The goal is . The path is . It takes moves to reach the goal.

Function Description
Complete the minimumMoves function in the editor.

minimumMoves has the following parameter(s):

  • string grid[n]: an array of strings that represent the rows of the grid
  • int startX: starting X coordinate
  • int startY: starting Y coordinate
  • int goalX: ending X coordinate
  • int goalY: ending Y coordinate

Returns

  • int: the minimum moves to reach the goal

Input Format

The first line contains an integer , the size of the array grid.
Each of the next lines contains a string of length .
The last line contains four space-separated integers,

Constraints

Sample Input

STDIN       FUNCTION
-----       --------
3           grid[] size n = 3
.X.         grid = ['.X.','.X.', '...']
.X.
...
0 0 0 2     startX = 0, startY = 0, goalX = 0, goalY = 2

Sample Output

3

Explanation

Here is a path that one could follow in order to reach the destination in steps:

.

  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy