#P2118. ACSL 2024-2025 Senior Division Contest #3 Manhattan Distance

ACSL 2024-2025 Senior Division Contest #3 Manhattan Distance

PROBLEM DESCRIPTION

In a Sinusoidal Triangle pattern, numbers are written from left-to-right, then right-to-left, then left-to-right, etc. with decreasing/increasing row widths. The width of rows decreases until there is just one number in the row; the width then increases until reaching the initial width; then decreases, and so on. The diagram below shows the first few rows of a 55-wide Sinusoidal Triangle pattern:

  1     2     3     4     5
  9     8     7     6
10  11  12
14  13
15
17  16
18  19  20
24  23  22  21
25  26  27  28  29
33  32  31  30
34  35  36
....

The Manhattan Distance between two numbers, MM and NN, in a Sinusoidal Triangle is how far the numbers are from each other in both the horizontal and vertical directions. You’ll be given the width of a Sinusoidal Triangle pattern, and two numbers, MM and NN, in the pattern. Your program must find the Manhattan Distance between numbers MM and NN.

INPUT FORMAT

Three positive integers: width, MM, and NN, each no more than 10001000.

OUTPUT FORMAT

Find the Manhattan Distance between MM and NN in the Sinusoidal Triangle with thespecified initial width.

SAMPLE

INPUT #1

5 17 2

OUTPUT #1

6

INPUT #2

3 24 25

OUTPUT #2

1

INPUT #3

7 40 30

OUTPUT #3

5

INPUT #4

6 5 100

OUTPUT #4

29

INPUT #5

8 200 180

OUTPUT #5

4

INPUT #6

4 100 120

OUTPUT #6

8

EXPLANATION

Sample #1 Explanation

Build the pattern as shown above. The Manhattan Distance between 1717 and 22 is 66: go horizontally 11 column and vertically 55 rows to get from one number to the other.