Write a method startingPoints to determine number of starting positions on the l
ID: 3824259 • Letter: W
Question
Write a method startingPoints to determine number of starting positions on the left side of the grid that have a path connecting to the right side of the grid. A path consists of a sequence of horizontally and vertically adjacent 'x' characters. The grid contains only '.' and 'x' characters.
You can write startingPoints in any way, but here are some hints:
Start at every grid location on the left and explore paths from it using a helper method.
Exploring a grid location means: if it's off the grid stop, if it doesn't contain an 'x' stop
Exploring a grid location on the right means: if it's an x, you've found a new path.
Exploring a grid location means: if it contains an 'x' it might be part of a path. Mark it as explored, check four neighbors, unmark it as explored.
--------------Constraints---------------
grid has between 1 and 16 rows, inclusive.
There are between 1 and 10 columns in the grid, inclusive.
Each character in each element of String[] grid can only be one of the following: a period (representing blocked space that does not connect) or an X (representing open space that can be on a path).
The enclosure is perfectly rectangular. That is, each element of String[] grid will be of the same length.
--------------------------------------
skeleton code is provided below
ublic class GridSolve {
public int startingPoints(String[] grid) {
// replace with working code
return 0; } }
Examples:
return 2
Returns 4. The paths do not need to be distinct.
Explanation / Answer
please find the code for the function
public class GridSolve {
public int startingPoints(String[] grid) {
int i=0;
for(int j=0;j<grid.length;j++){
if(grid[j].charAt(0) == 'X')
i++;
}
return i;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.