## Tree Cutting

View as PDF

Points: 12 (partial)
Time limit: 2.5s
Memory limit: 64M

Author:
Problem type

Tatsumi and Mine have decided to go out and buy a nice fir tree to celebrate their first holiday season together. They've headed to a nearby forest, and are now observing a gathering of trees rows by columns . Each tree has a height , and they want to take the tallest one home. If multiple trees satisfy the condition of tallest, they would like to pick the one closest to them in regard to Euclidean distance. The layout of the forest is such that no pair of tallest trees will ever be equidistant to Tatsumi and Mine.

However, the trees are packed so tight that to get to the tallest tree they may have to blast cut their way through a number of trees. Being conscious of the environment, they wish to minimize the total heights of the trees they clear. Knowing that they can only walk in cardinal directions, how many trees must they remove to arrive at their desired tree? The sum of the heights of the trees you cut must be the minimal over all possible sets of trees to cut to get to the tallest tree. Additionally, you should also minimize the number of trees summing to that minimal height in the event of a tie.

#### Scoring

For of the test cases, all trees will have one of only two possible values. For the rest, .

#### Input Specification

The first line will contain the integers and , separated by a single space.
The next lines will contain space-delimited integers , the height of the tree at .
A . denotes that there is no tree at that position, and the couple start off at the position marked with an X.

#### Output Specification

One integer, the number of trees Tatsumi and Mine will have to remove to get to their desired tree.

#### Sample Input 1

5 5
3 . . 2 3
. . 2 2 2
. 2 2 2 2
2 2 2 2 2
. . . . X

#### Sample Output 1

2

#### Explanation of Output for Sample Input 1

As in of the test cases, trees only come in 2 sizes: and . Therefore, the couple are after a tree where . Two trees satisfy this condition, but the one in the top-right corner is the closest to their starting point. To arrive at it, 2 trees of must be cut down.

#### Sample Input 2

4 8
1 1 1 1 1 1 1 9
. . 1 1 5 1 3 4
. . . . . . . .
X . . 8 7 6 . .

#### Sample Output 2

3

#### Explanation of Output for Sample Input 2

The tallest tree is that where , and to arrive at it 3 trees of must be cut. Alternatively, one tree of could be cut, but that does not minimize the heights of the trees cut as .

## Comments

• commented on Dec. 25, 2014, 8:21 p.m.

In Sample Output 1 we are minimizing the number of trees cut; in Sample Output 2 we are minimizing the heights of the trees cut.

• commented on Dec. 25, 2014, 8:49 p.m.

We are trying to minimize the sum of heights, but we do not actually have to output this minimal sum (it is a bit strange, I know).

Instead, after we have the minimal height, call it , we want to find the minimum number of trees on a path from the starting point to the target tree that sum to the minimum .