We want to send a soldier into a mine field with a directional metal
detector. The field is an n x n grid, and the soldier will move horizontally (east
or west) or
vertically (north or south), never leaving the field. His metal
detector can be pointed in any of the cardinal directions (east, north, west,
or south) and will beep if it senses any mine in that direction, no matter
how far away the mine is. Only mines that are exactly in the row or column down
which the sensor is pointed
The soldier can move one square at a time in any of the four directions,
pointing his sensor in various directions. But he will not move onto a square
if his sensor beeps when pointed in that direction, unless he has previously
visited that square. He will continue this
process, visiting and revisiting squares as needed, until he is sure that
he has visited every square that he can.
We want to see how well we will be
able to do using this type of sensor. Create a class LandMines that
contains a method numClear that takes the actual layout of mines in a
minefield as input and that returns the number of squares that the
soldier can safely visit.
The layout shows the mine field as a String, starting with the top row.
'-' denotes a square with no mine and 'M' denotes a square that contains a mine.
The soldier will always start in the northwest corner (the leftmost square in
the top row), which will never contain a mine.