BishopMove
SRM 628 · 2014-06-16 · by w10d
Problem Statement
Janusz is learning how to play chess. He is using the standard chessboard with 8 rows and 8 columns. Both the rows and the columns are numbered 0 through 7. Thus, we can describe each cell using its two coordinates: (row, column).
Janusz recently learned about one of the chess pieces: the bishop. The bishop is a piece that moves diagonally by an arbitrary number of cells. Formally, if a bishop is currently on the cell (r,c) of an empty chessboard, the set of all cells reachable in a single move contains the following cells:
- All cells of the form (r+k,c+k), where k is a positive integer.
- All cells of the form (r+k,c-k), where k is a positive integer.
- All cells of the form (r-k,c+k), where k is a positive integer.
- All cells of the form (r-k,c-k), where k is a positive integer.
Janusz took an empty chessboard and he placed a single bishop onto the cell (r1,c1). He now wants to move it to the cell (r2,c2) using as few moves as possible.
You are given the
Constraints
- r1,c1,r2,c2 will be between 0 and 7, inclusive.
4 6 7 3 Returns: 1
The bishop can go from (4,6) to (7,3) in a single move.
2 5 2 5 Returns: 0
The bishop is already where it should be, no moves are necessary.
1 3 5 5 Returns: 2
In the first move Janusz can move the bishop to the cell (4,6). Please note that this is the largest possible return value: whenever there is a solution, there is a solution that uses at most two moves.
4 6 7 4 Returns: -1
If the bishop starts at (4,6), it can never reach (7,4).
2 1 7 6 Returns: 1
Submissions are judged against all 62 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class BishopMove with a public method int howManyMoves(int r1, int c1, int r2, int c2) · 62 test cases · 2 s / 256 MB per case