Reference no: EM132206418
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:
grid = { "...x....x", "...x.....", "...xxx...", "x..x.x.xx", "xxxx.xxx.", "...x.....", "........." }
return 2
grid = { "x......x", "x..x.xxx", "x..x.x..", "xxxxxx.." }
Returns 4. The paths do not need to be distinct.
grid = { "...x....", "...x....", "...x....", "...x...." } Returns 0