As a member of the Straw Hat Pirates, you have sailed through the Grand Line
searching for the legendary treasure known as One Piece and encountered Blackbeard,
a notorious pirate captain leading a dangerous crew on an island used as a
stronghold for attacks on other pirate crews, prompting Captain Monkey D. Luffy's
resolve to end his reign of terror.
However, as your ship approaches the island, you face dangerous whirlpools and
currents, worsened by Blackbeard's activation of the Tremor - Tremor Devil Fruit,
causing rapid water level rise by inducing a Tsunami. This tsunami can potentially
sink a few islands. Since the height of this tsunami will be known a priori, one
should be able to calculate the number of sunken islands.
This whole landscape of islands is depicted as a 2D matrix. The values of cells of
this 2D matrix represent the height of that cell. All connected cells of similar
height form an island.
An island is deemed to be sunk, if an island is completely submerged by the
tsunami, that is if the tsunami height is greater than or equal to the height of
the island then that group of cells represents an island that has sunk.
Rules regarding tsunami sinking the island are as follows:
If Blackbeard uses his powers anywhere other than water cell (blue cells in the
diagrams below) then the waterlevel wont rise.
Hence Blackbeard may try to use his powers on water cell.
Starting cell where the water level begins to rise will be given as a part of
input.
Water will flow to the adjacent cells except diagonal cells if the water level is
not greater than tsunami height. This process will continue across the length and
the breadth of the matrix.
If a island whose height is taller than the tsunami height will be able to stop the
flow of water from entering that island.
Your mission is to determine how many islands have sunk due to the rising water
level, while your crewmates work to find a way to stop Blackbeard and lower the
water level.
Constraints
1 <= M, N <= 10
Tsunami height <=100
Height of Island < =100
Indexing of 2D matrix starts from 0
Input
First line contains two space separated integers M and N i.e. the size of the grid.
A grid of size M x N where each cell is either 1 to 100 (island with its height) or
'0' (water).
A tuple of size 2, representing the coordinates where Blackbeard used his power.
The last line contains an integer denoting the height of the tsunami caused by
Blackbeard.
Output
A single integer representing number of sunken islands. If none have sunk print
NONE.
Time Limit (secs)
1
Examples
Example 1
Input
5 5
4 4 0 0 0
3 4 0 2 2
3 3 0 2 2
0 0 1 2 2
0 0 1 1 1
2 2
Output
Explaination
Consider the following figure:
com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@2e185cd7:image1.png
At coordinate (2,2) Blackbeard activated his powers marked with black colour. So
that is where the water level starts rising and engulfs all the island adjacent to
it whose height is less than the tsunami height, then from those sunken island
water overflows to the next islands and so on. Therefore 3 islands with heights 1,
2 and 3 sinks, which are marked with different colours other than blue.
Example 2
Input
4 4
4 4 0 1
3 3 0 1
3 3 0 1
1 1 0 1
0 0
Output
NONE
Explaination
Consider the following figure:
com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@2e185cd7:image2.png
At coordinate (0,0) marked with red, Blackbeard activated his powers but as its not
a water cell, no island will sink.