This is a sequel to Chain Reaction (Part #1), with the same setup, but a different flavor.
As in the previous part, you will be given a rectangular array representing a "map" with three types of spaces:
The goal is simple: given a map, return the minimum number of bombs that need to be set off for all bombs to be destroyed by the chain reaction.
Let's look at some examples:
[
["+", "+", "+", "0", "+", "+", "+"],
["+", "+", "+", "0", "0", "+", "+"]
]
For the map above, the answer is 2; to explode all bombs you just need to set off one "+" bomb in the right cluster and one in the left cluster.
[
["x", "0", "x"],
["x", "x", "x"]
]
For the map above, the answer is 3; clearly setting off the three bottom "x" bombs is enough, and no less than three bombs suffice.
[
["x", "x", "x", "0", "x"],
["x", "x", "x", "x", "x"],
["x", "x", "x", "0", "x"]
]
For the map above, the answer is 3; setting off the three rightmost bombs in the middle row will do the trick.
min_bombs_needed([
["+", "+", "+", "0", "+", "+", "+"],
["+", "+", "+", "0", "0", "+", "+"]
]) ➞ 2
min_bombs_needed([
["x", "0", "x"],
["x", "x", "x"]
]) ➞ 3
min_bombs_needed([
["x", "x", "x", "0", "x"],
["x", "x", "x", "x", "x"],
["x", "x", "x", "0", "x"]
]) ➞ 3