Ramps
Time limit2sMemory limit512 MB
Count rows or columns where every cell has equal height, or where unit ramps of length L can bridge each height step of exactly 1.
- Level
Medium5 of 10
- Topics
- Implementation, Simulation, Array
- Solved
- No attempts yet
Problem
You are given an map. Each cell holds the height of the ground there.
You want to count how many roads on this map can be crossed. A road is one whole row or one whole column, and crossing it means going from one end to the other end. There are roads in total.
Look at this map with .

Its roads are drawn below.

A road can be crossed when every cell on it has the same height. Otherwise you can lay ramps to build a road you can cross. A ramp always has height 1 and length , and you never run out of ramps. A ramp connects a lower cell to a higher cell and must satisfy the following conditions.
- A ramp is laid on the lower side, and its bottom must touch consecutive cells along their whole length.
- The height difference between the lower cell and the higher cell must be 1.
- The cells under a ramp must be consecutive and must all have the same height.
A ramp cannot be laid in the following cases.
- Laying a ramp on a cell that another ramp already covers.
- The height difference between the lower cell and the higher cell is not 1.
- The cells on the lower side do not all have the same height, or of them are not consecutive.
- The ramp runs outside the map.
The picture below shows where a ramp can be laid when .

The next picture shows cases where a ramp cannot be laid.

Number those four cases 1 to 4 from the left. Case 1 fails because the height difference is not 1, case 2 because the bottom of the ramp does not touch the cells, case 3 because the ramps overlap, and case 4 because the ramp is laid at a slant.
For the map shown at the top with , the roads that can be crossed are drawn in blue and the roads that cannot are drawn in red.

Given the map, write a program that counts the roads that can be crossed.
Input
The first line contains () and (). Each of the next lines contains numbers describing the map. The height of a cell is a natural number not greater than 10.
Output
Print the number of roads that can be crossed on the first line.