Restoration of Boebeis Lake
Lake Karla (Boebeis) in Thessaly was created by the waters from the drainage of the surrounding mountainous masses to the Pineios River, as well as the floodwaters of the Pineios, which is situated higher than the lake. In 1962, its drainage of acres began without, however, creating productive arable land, while serious environmental consequences occurred. Nowadays, the Greek state, realizing the importance of wetlands, proceeds with the restoration of approximately acres of the lake.
With considerable simplification, this specific area can be approached with an NxN grid of equal-height squares. Each square is characterized by its altitude. In the diagram below (left), an example for is shown.
Depending on the volume of the drainage, squares with an altitude equal to or lower than the equivalent altitude of the drainage are flooded. For example, for drainage of equivalent altitude , the flooded squares are shown in red in the previous diagram (right). These are the squares with an altitude less than or equal to . The remaining squares that do not flood form five unified regions, meaning five areas consisting of squares, each sharing one of its sides with another. The five unified regions are shown in the diagram above (right) with colors: gray, blue, orange, yellow, and green.
Problem:
Given the geography of the lake and the equivalent drainage altitude, find the number of unified regions that do not flood.
Input Files:
The input files with the name karla.in are text files with the following structure: The first line contains two integers and separated by a space , where is the dimension of the grid and is the equivalent drainage height. Each of the next lines contains integers separated by a space , the altitudes of the squares in the corresponding row of the grid.
Output Files:
The output files with the name karla.out are text files with the following structure: They have exactly one line containing an integer . This number expresses the count of unified regions that do not flood.
Example 1:
STDIN (karla.in)
6 8 2 6 2
3 2 3 4 6
6 7 3 3 2
7 2 5 3 6
8 9 5 2 7
STDOUT (karla.out)
Example 2:
STDIN (karla.in)
9 9 9 9 9 9 9 9 9 9
9 8 8 8 8 8 8 8 8 9
9 8 7 7 7 7 7 7 8 9
9 8 7 6 6 6 6 7 8 9
9 8 7 6 5 5 6 7 8 9
9 8 7 6 5 5 6 7 8 9
9 8 7 6 6 6 6 7 8 9
9 8 7 7 7 7 7 7 8 9
9 8 8 8 8 8 8 8 8 9
9 9 9 9 9 9 9 9 9 9
STDOUT (karla.out)
Maximum Execution Time: 2 sec
Equivalent altitude is the height of a container with a corresponding surface area that would be filled with the drainage.
Comments