PDP-25 (2013) - Phase B' Junior High School (karla)

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 2.0s
Memory limit: 64M

Author:
Problem types
Allowed languages
C, C++, Java, Pascal, Python
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 130.000 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 78.000 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 N = 5 is shown.

pdp-2013-BG-fig-1.svg

Depending on the volume of the drainage, squares with an altitude equal to or lower than the equivalent altitude^1 of the drainage are flooded. For example, for drainage of equivalent altitude 4, the flooded squares are shown in red in the previous diagram (right). These are the squares with an altitude less than or equal to 4. 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 N and M separated by a space (1 \le N,\;M \le 100), where N is the dimension of the grid and M is the equivalent drainage height. Each of the next N lines contains N integers x_{ij} separated by a space (1 \le x_{ij} \le 1000), 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 K\;(0 \le K < N^2). This number expresses the count of unified regions that do not flood.

Example 1:

STDIN (karla.in)

5 4
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)

5


Example 2:

STDIN (karla.in)

10 6
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)

1


Maximum Execution Time: 2 sec


^1 Equivalent altitude is the height of a container with a corresponding surface area that would be filled with the drainage.


Comments

There are no comments at the moment.