- Interview Preparation Kits
- 1 Month Preparation Kit
- Week 4
- Castle on the Grid
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.
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.
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
- int: the minimum moves to reach the goal
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,
STDIN FUNCTION ----- -------- 3 grid size n = 3 .X. grid = ['.X.','.X.', '...'] .X. ... 0 0 0 2 startX = 0, startY = 0, goalX = 0, goalY = 2
Here is a path that one could follow in order to reach the destination in steps: