Connection Status:
Competition Arena > SilverDistance
SRM 456 · 2009-12-22 · by rng_58 · Math
Class Name: SilverDistance
Return Type: int
Method Name: minSteps
Arg Types: (int, int, int, int)
Problem Statement

Problem Statement

In Japanese Chess, there is a piece called Silver. A Silver piece located in cell (x,y) can move to any of the following cells in one step: (x-1,y+1), (x,y+1), (x+1,y+1), (x-1,y-1), (x+1,y-1). In other words, it can move one cell in any of the four diagonal directions, or it can move one cell vertically in the positive y direction.



Initially, there's a Silver piece in cell (sx,sy) of an infinitely large board. Return the minimal number of steps required to move to cell (gx,gy).

Constraints

  • sx, sy, gx and gy will each be between -1,000,000 and 1,000,000 inclusive.
Examples
0)
1
0
1
9
Returns: 9

Move up vertically in the positive y direction 9 times.

1)
0
0
-4
3
Returns: 5

Follow the path : (0,0) -> (-1,1) -> (-1,2) -> (-2,3) -> (-3,2) -> (-4,3)

2)
0
0
5
8
Returns: 8

Move up vertically in the postive y direction 3 times, then move diagonally up and to the right 5 times.

3)
-487617
826524
892309
-918045
Returns: 1744571
4)
-27857
31475
-27857
31475
Returns: 0

The Silver is already in the goal.

Submissions are judged against all 143 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class SilverDistance with a public method int minSteps(int sx, int sy, int gx, int gy) · 143 test cases · 2 s / 256 MB per case

Submitting as anonymous